簡體   English   中英

使用 live() 時,有沒有辦法強制事件僅在給定元素(每個元素,而不是整個文檔)上觸發一次?

[英]is there a way to force event to fire only once on given element (per element, not whole document) when using live()?

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

我知道有一個one() function 和 jquery 但我不知道如何將它與上面的代碼集成。

$('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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM