简体   繁体   English

删除元素的所有onclick事件

[英]Remove All onclick Events for an Element

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. 是我当前的代码,但它不会删除onclick事件。 I have no idea what they will be since this is a greasemonkey script. 我不知道它们会是什么,因为这是一个油脂脚本。

Your code only deals with events added by element.onclick case. 您的代码仅处理element.onclick案例添加的事件。 What about events added with addEventListener (for standards compliant browsers) and attachEvent (for IE)? 使用addEventListener(适用于标准的浏览器)和attachEvent(适用于IE)添加的事件如何?

You need to use removeEventListener and detachEvent to remove events as well as setting .onclick to null. 您需要使用removeEventListener和detachEvent删除事件以及将.onclick设置为null。 Then all your bases will be covered. 然后你的所有基地都将被覆盖。

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. 请记住,如果使用jQuery创建click事件,jQuery将删除click事件。 the function given in the article will remove both those created with jQuery and those that were not. 本文中给出的函数将删除使用jQuery创建的函数和不使用jQuery创建的函数。 The function given is this: 给出的函数是这样的:

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. 虽然这只能解决点击事件,但您可以轻松修改它以处理其他人或所有事件。

That code doesn't work because of GM's sandbox. 由于GM的沙箱,该代码不起作用 links is in an XPCNativeWrapper . 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() )。

I am guessing you have either an href or a JavaScript function being called on the onClick for an <a> link. 我猜你在onClick为一个<a>链接调用了一个href或一个JavaScript函数。

You can remove either of these by removing the href tag, the onClick event or in this case both of them. 您可以通过删除href标记, onClick事件或在这种情况下删除它们中的任何一个来删除其中任何一个。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM