[英]jQuery live() method fires only once
I have seen similar quesitons answered on the website, but I can't find a fix that works for me :( 我在网站上看到过类似的问题,但找不到适合我的修复方法:(
I am trying to use jQuery's live() method to trigger an event several time in a page's division after it's content is refreshed, when clicking on a link within the division itself. 我试图使用jQuery的live()方法在页面内容中的内容刷新后单击页面本身中的链接时触发一次事件。
The content is first loaded in "divdroite" after a click on a link within "divgauche", and then I'd like clicks within "divdroite" to refresh its own content any number of times. 单击“ divgauche”中的链接后,首先将内容加载到“ divdroite”中,然后我希望在“ divdroite”中单击以刷新其自身内容多次。
The code is as follows : 代码如下:
$j("body").on('click', "#divgauche a", function (event1) {
$j("#divdroite").load((this.href + " #courscontainer"), function () {
$j('#divdroite .collapsible').on('click', function (g) {
$j('>*:has(*)', g.target).slideToggle(300);
g.preventDefault();
g.stopPropagation();
});
});
event1.preventDefault();
event1.stopPropagation();
});
$j("#divdroite a").live('click', function (event2) {
$j("#divdroite").load((this.href + " #courscontainer"), function () {
$j('#divdroite .collapsible').on('click', function (h) {
$j('>*:has(*)', h.target).slideToggle(300);
h.preventDefault();
h.stopPropagation();
});
});
event2.preventDefault();
event2.stopPropagation();
});
Any help would be greately appreciated. 任何帮助将不胜感激。
Thanks!! 谢谢!!
Try adding the .complete callback on the .load event to apply a click event to the element you just loaded. 尝试在.load事件上添加.complete回调,以将click事件应用于刚刚加载的元素。
$j("#divdroite").load((this.href + " #courscontainer"), function ()
{
$j('#divdroite .collapsible').on('click',function(h)
{
$j('>*:has(*)',h.target).slideToggle(300);
h.preventDefault();
h.stopPropagation();
});
});
event2.preventDefault();
event2.stopPropagation();
})
.complete(function(){
// add code your click event code here that will apply to the loaded div
$("#coursecontainer").on('click',someFunction);
});
It's hard to see exactly what's going on, however ... 很难确切知道发生了什么,但是...
In the same way that you delegate clicks on a
elements, you should also be able to delegate clicks on .collapsible
elements. 在你委托的点击以同样的方式
a
元素,你也应该能够下放的点击.collapsible
元素。
Using .on(eventType, selector, fn)
syntax (twice), the code should be something like this : 使用
.on(eventType, selector, fn)
语法(两次),代码应如下所示:
$j("#divdroite").on('click', "a", function (e) {
e.preventDefault();
e.stopPropagation();
$j("#divdroite").load((this.href + " #courscontainer"));
}).on('click', '.collapsible', function (e) {
e.preventDefault();
e.stopPropagation();
$j('>*:has(*)', e.target).slideToggle(300);
});
The only assumption there is that #divdroite
is a static element that doesn't get removed/replaced (by some other piece of javascript). 唯一的假设是
#divdroite
是静态元素,不会被删除/替换(通过其他JavaScript元素)。 If it still doesn't work, then I'm guessing that's what's happening. 如果仍然不起作用,那么我猜这就是正在发生的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.