jQuery - 从元素中删除onclick

时间:2022-08-27 12:41:14

I have the following setup:

我有以下设置:

   <div id="whatever" onclick="dostuff()">Super Mega Giga Button or Whatever</div>

   <!-- Import jquery and whatever other libs I need-->

   <script>
        $("#whatever").click(function() { //do something });
   </script>

Now what I need to do is at some point to remove all the onclick events.

现在我需要做的是删除所有onclick事件。

I tried $("#whatever").unblind('click'); but this removes only the event added with jquery, the dostuff() function is still called.

我试过$(“#whatever”)。unblind('click');但是这只删除了用jquery添加的事件,仍然调用了dostuff()函数。

Any idea how can I remove all at once?

知道如何一次删除所有内容?

P.S. Don't start asking why would some1 have inline onclick and also listeners.

附:不要开始问为什么some1有内联onclick和听众。

1 个解决方案

#1


11  

You can use the removeAttr() method:

您可以使用removeAttr()方法:

Remove an attribute from each element in the set of matched elements.

从匹配元素集中的每个元素中删除一个属性。

$('#whatever').removeAttr('onclick')
$("#whatever").click(function() { 
  //do something 
});

#1


11  

You can use the removeAttr() method:

您可以使用removeAttr()方法:

Remove an attribute from each element in the set of matched elements.

从匹配元素集中的每个元素中删除一个属性。

$('#whatever').removeAttr('onclick')
$("#whatever").click(function() { 
  //do something 
});