有没有办法在使用live()时强制事件只在给定元素(每个元素,而不是整个文档)上触发一次?

$('div.myclass').live('click',function() { alert('i should respond only once'); }); 

我知道有一个带jquery的one()函数,但我无法想象如何将它与上面的代码集成。

 $('div.myclass').live('click',function() { if($(this).attr('clicked') != 'yes') { alert('i should respond only once'); $(this).attr('clicked', 'yes'); } }); 

live()单击处理程序中,您可以直接在调用e.stopPropagation()的元素上bind()事件。 因此,在下一次单击时,事件将无法冒泡到文档,实际上它会触发实时事件。

演示: http : //jsfiddle.net/kKHJm/

 $('li').live('click',function(){ $(this).click(function(e){ e.stopPropagation(); }); // Do stuff once here });