How to resume click event in jQuery
I have used unbind
to detach the
the click event for certain condition. After that I want to resume that click event. How to do this?
jQuery('#div1 span').click(function(){
if(jQuery('#div1 a:last').hasClass('current'))
{
jQuery('#div1 span').unbind(click);
}
}
jQuery('#div1 a').click(function(){
jQuery(this).addClass('current');
//here i want to resume click event for #div1 span is this possible
});
<div id ="div1"><span>&开发者_Python百科lt;/span><a></a><a></a><a></a></div>
Here when the current class is not with last anchor I want to trigger click on span.
You can explicitly .bind()
help and .unbind()
help a method:
function my_click_handler(event) {
// click handler code
}
// bind that handler to #element
$('#element').bind('click', my_click_handler);
// remove the handler
$('#element').unbind('click', my_click_handler);
// bind again
$('#element').bind('click', my_click_handler);
It might be easier if you just checked for a variable like clickHandlerEnabled
in your onclick event handler instead of unbinding and re-binding the event handler.
If it's element-specific you could store it in its `data()``:
$('.elem').click(function(e) {
if($(this).data('clickDisabled')) {
return;
}
});
And to set it:
$('#someElem').data('clickDisabled', true /* or false to re-enable clicks */);
if you call Jquery click event again under other condition to resume it, jquery will automatically bind it again.
精彩评论