ホバー状態で動的に作成された要素に問題があります。新しく作成したhtml要素にカーソルを合わせると、機能しません。
これが私の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!');
});
私もこのコードを試しました:
$('.hover').on({
mouseenter : function() {
alert('you hovered the button!');
},
mouseleave : function() {
alert('you removed the hover from button!');
}
});
ここに示されているようにhttp://api.jquery.com/on/ですが、それでも運がありません。デモもあります:http://jsfiddle.net/BQ2FA/