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

前端 未结 2 1699
遥遥无期
遥遥无期 2020-12-22 12:16
$(\'div.myclass\').live(\'click\',function() {
    alert(\'i should respond only once\');
});

I know that there is a one() function wi

2条回答
  •  时光说笑
    2020-12-22 13:10

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

提交回复
热议问题