Unbinding inline onClick not working in jQuery?

前端 未结 2 911
-上瘾入骨i
-上瘾入骨i 2020-12-11 00:35

Okay so, I\'m wondering how to unbind an inline onclick event in jQuery. You\'d think .unbind() would work, however it doesn\'t.

To test this for you

相关标签:
2条回答
  • 2020-12-11 01:27

    unbind unbinds event handlers registered with bind, not ones assigned via the onclick or other DOM0 attributes. From the docs:

    Any handler that has been attached with .bind() can be removed with .unbind().

    Edit: You unbind DOM0 handlers by clearing the relevant attribute on the element. I had to go look up the jQuery way of doing that: removeAttr.

    0 讨论(0)
  • 2020-12-11 01:31

    jQuery's unbind won't work on onclick attributes - it only works for functions that were added via bind and thus are available in $(...).data('events'). You have to use removeAttr to remove onclick.

    Read this question for more info.

    0 讨论(0)
提交回复
热议问题