Javascript jquery从1.7迁移到1.10.1
我正在尝试从jQuery1.7迁移到1.10,而live函数不再工作Javascript jquery从1.7迁移到1.10.1,javascript,jquery,event-handling,Javascript,Jquery,Event Handling,我正在尝试从jQuery1.7迁移到1.10,而live函数不再工作 $("#detail_content").on("click", ".close", function (a) { // is ignored //$("#detail_content .close").live("click", function (a) { //works fine with migrate console.log("click"); }); 稍后将通过ajax加载div.detail_内容,但如果
$("#detail_content").on("click", ".close", function (a) { // is ignored
//$("#detail_content .close").live("click", function (a) { //works fine with migrate
console.log("click");
});
稍后将通过ajax加载div.detail_内容,但如果我从.live更改为.on,关闭按钮将不再起作用
我认为代表团失踪了
有什么想法吗?看起来像是#详细内容
也是动态的,然后试试看
$(document).on("click", "#detail_content .close", function (a) { // is ignored
//$("#detail_content .close").live("click", function (a) { //works fine with migrate
console.log("click");
});
您应该使用任何最近的static父元素(或最后使用
body
):
因此,如果您有如下标记:
<body>
...
<div id="container">
...
<div id="detail_content"><button class="close">Close</button></div>
</div>
</body>
愿景,仅仅使用以下内容不一样吗
$(document).ready(function(){
$(".close").click(function(){
console.log("clicked");
}
});
上面的代码中是否存在速度较慢或效率较低的地方?jQuery 1.10及更高版本中不推荐使用.live()
方法。使用.on()
方法附加事件处理程序
因此,您可以使用此代码而不是.live()
我认为这对您很有用,在中您可以看到所有不推荐的方法,对于希望从1.7迁移到1.10的人来说也很有用它的可能副本不会被复制。语法还可以。在本例中,缺少的静态父对象是subject.thx,就是它。我是jquery的新手。使用mootools,您不需要静态元素。
$("#container").on("click", "#detail_content .close", function() { ... });
$(document).ready(function(){
$(".close").click(function(){
console.log("clicked");
}
});
$(document).on('click', '#detail_content .close', function(){
//your Code
});