JQuery:从“a”标签中删除“OnClick”事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8565210/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:41:51  来源:igfitidea点击:

JQuery: Remove "OnClick" events from 'a' tags

jquery

提问by Nate

This is a weird question. We have some production links down on our intranet. Some rouge javascript is returning a "false" on all links on our intranet homepage.

这是一个奇怪的问题。我们的内部网上有一些生产链接。一些 rouge javascript 在我们的 Intranet 主页上的所有链接上都返回“false”。

We don't have access to the source to re-build the control and fix this javascript.

我们无权访问源来重新构建控件并修复此 javascript。

So as a temporary band-aid I want to move all the "onClick" events on the links using jquery

因此,作为临时创可贴,我想使用 jquery 移动链接上的所有“onClick”事件

The links right now are like this:

现在的链接是这样的:

<a href="https://www.mysite.com/" onclick="AddHit('Header: mysite.com', 'http://www.mysite.com'); return false;" title="Mysite Home">www.Mysite.com</a>

I want to write some jquery to get ride of the "onclick='AddHit..." code.

我想编写一些 jquery 来使用“onclick='AddHit...”代码。

回答by Rondel

You can try this:

你可以试试这个:

$("a").removeAttr("onclick");

EditUpdating answer based on comments and jquery Docs:

根据评论和jquery 文档编辑更新答案:

Note: 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()

Best way to do this:

执行此操作的最佳方法:

$("a").prop("onclick", null);

回答by Laurent le Beau-Martin

Remove the attribute from all links that have it:

从所有拥有它的链接中删除该属性:

$('a[onclick]').removeAttr('onclick');

回答by Jay

According to JQuery documentation you should use .prop() to remove an inline onclick, as this avoids issues in Internet Explorer 6, 7, or 8.

根据 JQuery 文档,您应该使用 .prop() 删除内联 onclick,因为这可以避免 Internet Explorer 6、7 或 8 中的问题。

Try this

尝试这个

$element.prop("onclick", null);

I needed to remove the onclick of an tag after the first click so here is what I did:

我需要在第一次点击后删除一个标签的 onclick 所以这是我所做的:

onclick="$(this).prop('onclick', null);"

Hope it helps

希望能帮助到你

回答by Tony

$(function(){
    $('a[onclick*="AddHit"]').removeAttr('onclick');
}

This will remove all of them but you want to make sure that you don't remove it from unwanted links. A lot of stuff hooks into the onclick attribute, if you have a class you can verify before removing or the contents of the onclick it would be better. Would hate to watch the whole site stop working because needed JS didn't get called. This will remove any onclicks with the AddHit function in the onclick to keep you safer.

这将删除所有这些,但您要确保不会从不需要的链接中删除它。很多东西都与 onclick 属性挂钩,如果你有一个类,你可以在删除之前验证或者 onclick 的内容会更好。不想看到整个站点停止工作,因为没有调用所需的 JS。这将使用 onclick 中的 AddHit 函数删除任何 onclick,以确保您更安全。

回答by Shawn Chin

This removes the onclickattribute for all links that has an onclickvalue that starts with "AddHit(" (leaving other links untouched).

这将删除值以“ ”开头的onclick所有链接的属性(保留其他链接不变)。onclickAddHit(

$('a[onclick^="AddHit("]').removeAttr("onclick");

回答by Stefan

You can do something like this;

你可以做这样的事情;

$('a[onclick]').removeAttr('onclick');

回答by lumbric

Not tested, but I think this should work:

未经测试,但我认为这应该有效:

$(document).ready(function(){
    $('a').removeAttr("OnClick");
});

More info: http://api.jquery.com/removeAttr/

更多信息:http: //api.jquery.com/removeAttr/

回答by SNS - Web et Informatique

Keep this in mind, if your 'onclick' attribute is ONLINE AND in a MODAL like (bootstrap)

请记住这一点,如果您的 'onclick' 属性是 ONLINE 并且在像 (bootstrap) 这样的 MODAL 中

like this : (in modal)

像这样:(在模态中)

<a id="myDiv" href="#" onclick="doSomething();">Click here !</a>

-> Do mention to the <body>tag !

-> 请提及<body>标签!

in jQuery (with body in first selector) :

在 jQuery 中(在第一个选择器中带有主体):

$('body #myDiv').prop('onclick', null);

I searched for a few hours ...

我找了几个小时...

回答by ALH

Use .unbind()method or off()to get rid of that.

使用.unbind()方法或off()摆脱它。