开发者

jQuery unbind, then bind again

开发者 https://www.devze.com 2023-01-31 07:00 出处:网络
i\'ve my function (i need to unbind later): $(\"#closebtn\").click(function(){ $.address.value(\'/x\');

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

精彩评论

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