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
});
精彩评论