开发者

jQuery unbind, then bind again

i've my function (i need to unbind later):

$("#closebtn").click(function(){
    $.address.value('/x');
});

I unbind it after an event:

$("#closebtn").unbind("click");

Now I want to associate my "click" function again to my #c开发者_JAVA百科losebtn, how can i do ? thank you d


Assign your function to a variable

var closebtn = function() {
    $.address.value('/x');
};

Then you can bind with the variable

$("#closebtn").click(closebtn);


If it is actually a button, could you just disable it, rather than unbind/re-bind. Or, add an if statement so the function returns false when you don't want it to 'fire' - I think that would be more efficient than bind/un-bind/rebind, which is fairly expensive.

Something like this, maybe?

$("#closebtn").click(
    function() {
        if (inEvent) {
            return false
        }
        $.address.value('/x')
    })

$('someOtherElement').click(
    function() {
        inEvent = 1
        // ...
        // do the processing where you don't
        // want the function to fire
        .. ...
        inEvent = 0
    })
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜