Jquery 实时处理不适用于事件。StopRopAgation();
我正在使用Jquery 实时处理不适用于事件。StopRopAgation();,jquery,live,stoppropagation,Jquery,Live,Stoppropagation,我正在使用event.stopPropagation(),但在父级上使用.live()处理程序时遇到问题 //does not work $("#testlink").live({ click: function(event) { alert('testlink'); } }); //works great! $("#testlink").click(function() { alert('testlink'); }); 我必须使
event.stopPropagation()
,但在父级上使用.live()
处理程序时遇到问题
//does not work
$("#testlink").live({
click: function(event)
{
alert('testlink');
}
});
//works great!
$("#testlink").click(function() {
alert('testlink');
});
我必须使用.live()
,因为内容是用AJAX加载的
有人能帮我解决这个问题吗
如何使用更多这样的类或ID
$('#div1', '#div2', '#div3').on('click', 'a.testlink', function(){
alert('testlink');
});
这是怎么可能的?如果内容是动态生成的,则使用(如@techfoobar所述,在1.7版中不推荐使用,并在1.9版中删除):
$('#container').on('click', '#testlink', function(){
alert('testlink');
});
其中,
#container
是一个包含动态加载的#testLink
元素的元素(您可以使用文档
,但在此处使用container元素可以缩小搜索范围并提高效率) live()
已弃用。改用.on()
。最好的解决方案是在这里使用#容器元素。这将把事件附加到#container
元素中的任何链接,减少了检查整个文档
元素树的范围,并提高了效率。@PalashMondal当然,但我不知道#container
元素当前是什么,因为OP没有提供足够的细节(或任何类型的标记):)至少你可以在你的答案中写下这句话,作为一个建议:)@PalashMondal当然,不会伤害你的。现在更新