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

rsk*_*k82 0 javascript jquery

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

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

Sen*_*kin 5

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

});
Run Code Online (Sandbox Code Playgroud)

  • +1如果你做了`$(this).data('clicked',true)`和`if($(this).data('clicked')){`而不是使用任意文本属性. (5认同)