微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使jQuery 1.7 .on()悬停?

我在悬停状态下动态创建元素时遇到问题.当我将鼠标悬停在新创建的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/

解决方法

这不是正确的语法.

使用它来监听动态创建的“.hover”元素的事件:

$(document).on('mouseenter','.hover',function(){
         alert('you hovered the button!');
}).on('mouseleave',function() {
        alert('you removed the hover from button!');
});

原文地址:https://www.jb51.cc/jquery/179049.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐