JQuery Unbind and then bind

后端 未结 3 738
萌比男神i
萌比男神i 2020-12-11 13:03

I\'ve found lots of examples on this topic.. however, I do not fully understand. Does my logic seem correct? I have a div as button (class of .myClass) and I don\'t want i

3条回答
  •  情深已故
    2020-12-11 13:50

    click(callMyFunction) is shorthand for .bind("click", callMyFunction) or .on("click", callMyFunction).

    To bind use on() to unbind use off(). Those are the preferred methods since jQuery 1.7. If you are using an older version use bind(), unbind(), or delegate() and undelegate()

    $('.myClass').off('click')
    

    and to bind

    $('.myClass').on('click', function(){});
    

    As mentioned in another post, using namespaces is an additional way to categorize your events such as click.mynamespace for example.

    See this post for much more detail on the different available binding methods and which were introduced when and have replaced what and why.

    Some more resources:

    • click()
    • unbind()
    • on()
    • off()

提交回复
热议问题