问题描述
我有一个场景,其中JQuery'on'& 现场"表演不一样.也许有人可以指出原因.我在项目中使用JQuery 1.7.2,在此构建中,实时"已替换为开启".我在列表页面中使用以下代码.基本上,此页面具有字母栏,用户可以单击&将使用该姓氏加载所有客户端.我希望链接通过ajax执行.
I have a scenario where JQuery 'on' & 'live' do not perform the same. Perhaps someone can point out why. I am using JQuery 1.7.2 with my project and in this build, 'live' has been replaced with 'on'. I am using the following code in a listing page. Basically, this page has an alphabetical bar that the user can click & will load all the clients with that last name. I would like the link to execute via ajax.
代码:
$("a.listajax").on("click", function (e) {
e.preventDefault();
var url = $(this).attr("href");
$("div.content").load(url + " div.content");
return false;
});
这里的问题是,当我第一次加载页面并单击链接时,一切正常.该页面通过ajax加载.但是,此后,所有链接都将失去其绑定& ;;然后,如果我单击任何链接,就会加载整个页面.
The problem here is that when I first load the page and click on a link, everything works fine. The page gets loaded via ajax. However, after that all the links lose their bindings & then if I click on any links, I get an entire page loads.
我用实时"替换了打开",并且即使在随后的点击中,链接也开始表现良好.
I replaced the 'on' with 'live' and the links started behaving perfectly, even on subsequent clicks.
我想念什么?
推荐答案
一个并不能简单地将.live
替换为.on
.
One does not simply replace .live
with .on
.
$("a.listajax").live('click', function(e))
等效于:
$(document).on('click', 'a.listajax', function(e))
重要
如果所有.listajax
锚都有一个共同的祖先,并且不会从DOM中删除,则应使用(尽可能深的)而不是document
;这样可以提高性能.
If there's a common ancestor for all your .listajax
anchors that will not be removed from the DOM, you should use that (the deepest one possible) instead of document
; this will improve performance.
这篇关于jQuery的``开''与``活''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!