开发者

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

I know that there is a one() function with jquery but i can't figure how to integrate this with the code above.


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

});


In the live() click handler, you can bind() an event directly on the element which calls e.stopPropagation(). So on the next click, the event won't be able to bubble up to the document, where it actually triggers the live event.

Demo: http://jsfiddle.net/kKHJm/

$('li').live('click',function(){
    $(this).click(function(e){
        e.stopPropagation();
    });

    // Do stuff once here
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜