如何使用 JQuery 删除“onclick”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1687790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove "onclick" with JQuery?
提问by Steven
PHP code:
PHP代码:
<a id="a$id" onclick="check($id,1)" href="javascript:void(0)" class="black">Qualify</a>
I want to remove the onclick="check($id,1)
so the link cannot be clicked or "check($id,1)
won't be fired. How can I do it with JQuery?
我想删除onclick="check($id,1)
以便无法点击链接或“check($id,1)
不会被触发。我该如何使用 JQuery 来做到这一点?
回答by glmxndr
Old Way(pre-1.7):
旧方式(1.7 之前):
$("...").attr("onclick", "").unbind("click");
New Way(1.7+):
新方式(1.7+):
$("...").prop("onclick", null).off("click");
(Replace ... with the selector you need.)
(用您需要的选择器替换 ...。)
// use the "[attr=value]" syntax to avoid syntax errors with special characters (like "$")
$('[id="a$id"]').prop('onclick',null).off('click');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="a$id" onclick="alert('get rid of this')" href="javascript:void(0)" class="black">Qualify</a>
回答by acme
I know this is quite old, but when a lost stranger finds this question looking for an answer (like I did) then this is the best way to do it, instead of using removeAttr():
我知道这已经很老了,但是当一个迷路的陌生人发现这个问题正在寻找答案时(就像我一样),那么这是最好的方法,而不是使用 removeAttr():
$element.prop("onclick", null);
Citing jQuerys official doku:
"Removing an inline onclick event handler using .removeAttr() doesn't achieve the desired effect in Internet Explorer 6, 7, or 8. To avoid potential problems, use .prop() instead"
“使用 .removeAttr() 删除内联 onclick 事件处理程序在 Internet Explorer 6、7 或 8 中无法达到预期效果。为避免潜在问题,请改用 .prop()”
回答by John Slegers
Removing onclick
property
移除onclick
财产
Suppose you added your click event inline, like this :
假设您内联添加了点击事件,如下所示:
<button id="myButton" onclick="alert('test')">Married</button>
Then, you can remove the event like this :
然后,您可以像这样删除事件:
$("#myButton").prop('onclick', null); // Removes 'onclick' property if found
Removing click
event listeners
移除click
事件监听器
Suppose you added your click event by defining an event listener, like this :
假设您通过定义一个事件监听器来添加点击事件,如下所示:
$("button").on("click", function() { alert("clicked!"); });
... or like this:
...或像这样:
$("button").click(function() { alert("clicked!"); });
Then, you can remove the event like this :
然后,您可以像这样删除事件:
$("#myButton").off('click'); // Removes other events if found
Removing both
删除两者
If you're uncertain how your event has been added, you can combine both, like this :
如果您不确定您的事件是如何添加的,您可以将两者结合起来,如下所示:
$("#myButton").prop('onclick', null) // Removes 'onclick' property if found
.off('click'); // Removes other events if found
回答by Jaydeep Jadav
if you are using jquery 1.7
如果您使用的是 jquery 1.7
$('html').off('click');
else
别的
$('html').unbind('click');
回答by Nor Leyva López
It is very easy using removeAttr.
使用 removeAttr 非常容易。
$(element).removeAttr("onclick");
回答by bboydflo
After trying so hard with bind, unbind, on, off, click, attr, removeAttr, prop I made it work. So, I have the following scenario: In my html i have NOT attached any inline onclick handlers.
在使用绑定、解除绑定、打开、关闭、单击、attr、removeAttr、prop 如此努力之后,我让它工作了。所以,我有以下场景:在我的 html 中,我没有附加任何内联 onclick 处理程序。
Then in my Javascript i used the following to add an inline onclick handler:
然后在我的 Javascript 中,我使用以下内容添加内联 onclick 处理程序:
$(element).attr('onclick','myFunction()');
To remove this at a later point from Javascript I used the following:
为了稍后从 Javascript 中删除它,我使用了以下内容:
$(element).prop('onclick',null);
This is the way it worked for me to bind and unbind click events dinamically in Javascript. Remember NOT to insert any inline onclick handler in your elements.
这就是我在 Javascript 中动态绑定和取消绑定点击事件的方式。请记住不要在您的元素中插入任何内联 onclick 处理程序。
回答by Milan
What if onclick event is not directly on element, but from parent element? This should work:
如果 onclick 事件不是直接在元素上,而是来自父元素怎么办?这应该有效:
$(".noclick").attr('onclick','').unbind('click');
$(".noclick").click(function(e){
e.preventDefault();
e.stopPropagation();
return false;
});
回答by Vishnu Sharma
Try this if you unbind the onclick event by ID Then use:
如果您通过 ID 取消绑定 onclick 事件,请尝试此操作然后使用:
$('#youLinkID').attr('onclick','').unbind('click');
Try this if you unbind the onclick event by Class Then use:
如果您按类取消绑定 onclick 事件,请尝试此操作然后使用:
$('.className').attr('onclick','').unbind('click');