Tôi đang gặp sự cố với các yếu tố được tạo động trên trạng thái di chuột. Khi tôi di chuột trên phần tử html mới được tạo, nó không hoạt động.Làm thế nào để làm cho jQuery 1.7 .on() di chuột?
Dưới đây là mã của tôi HTML:
<button id="create">create new button</button>
<button class="hover">hover me</button>
<div></div>
jQuery:
var createBtn = $("#create");
createBtn.click(function() {
$('div').append('<button class="hover">new hover button</button');
return false;
});
$('.hover').hover(function() {
alert('you hovered the button!');
}, function() {
alert('you removed the hover from button!');
});
Tôi thậm chí đã cố gắng mã này:
$('.hover').on({
mouseenter : function() {
alert('you hovered the button!');
},
mouseleave : function() {
alert('you removed the hover from button!');
}
});
như đây http://api.jquery.com/on/, nhưng vẫn không có may mắn. Đây cũng là bản trình diễn: http://jsfiddle.net/BQ2FA/
có thể điều này sẽ giúp về việc bật/tắt: http://stackoverflow.com/questions/11283341/jquery-disable-click-until-animation-is-fully-complete –