Javascript 删除元素的所有 onclick 事件

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

Remove All onclick Events for an Element

javascriptgreasemonkey

提问by Steven

var links = document.body.querySelectorAll("p.sourcelinks a.individual_source_link");
for(var i=0;i<links.length;i++)
{
    links[i].onclick = null;
}

Is my current code, however it doesn't remove the onclick events. I have no idea what they will be since this is a greasemonkey script.

是我当前的代码,但是它不会删除 onclick 事件。我不知道它们会是什么,因为这是一个油脂猴子脚本。

回答by GordonM

Your code only deals with events added by element.onclick case. What about events added with addEventListener (for standards compliant browsers) and attachEvent (for IE)?

您的代码仅处理由 element.onclick 案例添加的事件。使用 addEventListener(对于符合标准的浏览器)和 attachEvent(对于 IE)添加的事件呢?

You need to use removeEventListener and detachEvent to remove events as well as setting .onclick to null. Then all your bases will be covered.

您需要使用 removeEventListener 和 detachEvent 来删除事件以及将 .onclick 设置为 null。然后你所有的基地都将被覆盖。

回答by Lucavai

This article would probably be useful:

这篇文章可能有用:

http://www.computerhowtoguy.com/how-to-use-the-jquery-unbind-method-on-all-child-elements/

http://www.computerhowtoguy.com/how-to-use-the-jquery-unbind-method-on-all-child-elements/

One part in particular is a recursive function that removes all click events. Remember that jQuery will remove click events IF the click event was created using jQuery. the function given in the article will remove both those created with jQuery and those that were not. The function given is this:

特别是一个部分是删除所有点击事件的递归函数。请记住,如果单击事件是使用 jQuery 创建的,则 jQuery 将删除单击事件。文章中给出的函数将删除那些用 jQuery 创建的和那些不是的。给出的函数是这样的:

function RecursiveUnbind($jElement) {
    // remove this element's and all of its children's click events
    $jElement.unbind();
    $jElement.removeAttr('onclick');
    $jElement.children().each(function () {
        RecursiveUnbind($(this));
    });
}

You would call the function like this:

你会像这样调用函数:

RecursiveUnbind($('#container'));

That function takes a jQuery object parameter, but you could easily change it up to pass a string as the name of the ID for the element, or however you think is best.

该函数接受一个 jQuery 对象参数,但您可以轻松地将其更改为传递一个字符串作为元素的 ID 名称,或者您认为最好的方式。

While this only addresses click events you could easily modify it to handle others or all.

虽然这仅解决点击事件,但您可以轻松修改它以处理其他事件或所有事件。

回答by Brock Adams

That code doesn't work because of GM's sandbox. linksis in an XPCNativeWrapper.

由于 GM 的沙箱,links该代码不起作用在 XPCNativeWrapper 中

To get around this use setAttribute(), like so:

为了解决这个使用setAttribute(),像这样:

var links = document.body.querySelectorAll("p.sourcelinks a.individual_source_link");
for(var i=0;i<links.length;i++)
{
    links[i].setAttribute ("onclick", null);
}


Note that click handlers that are set other ways, will need to be cleared other ways (removeEventListener(), for example).


请注意,以其他方式设置的点击处理程序需要以其他方式清除(removeEventListener()例如)。

回答by codingbadger

I am guessing you have either an hrefor a JavaScript function being called on the onClickfor an <a>link.

我猜你hrefonClickfor<a>链接上调用了一个或一个 JavaScript 函数。

You can remove either of these by removing the hreftag, the onClickevent or in this case both of them.

您可以通过删除href标签、onClick事件或在这种情况下两者来删除其中任何一个。

    for(var i=0;i<links.length;i++)
    {

        links[i].href='#';
        links[i].onclick = '';
    }