最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove All onclick Events for an Element - Stack Overflow

programmeradmin1浏览0评论
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.

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.

Share Improve this question asked Feb 7, 2011 at 7:54 StevenSteven 1,0613 gold badges10 silver badges14 bronze badges 2
  • "Is my current code (what) , however it doesn't remove the onclick events." !!! – user284291 Commented Feb 7, 2011 at 8:02
  • "It is my current code, ..." I think – w35l3y Commented Feb 12, 2011 at 3:14
Add a comment  | 

5 Answers 5

Reset to default 7

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)?

You need to use removeEventListener and detachEvent to remove events as well as setting .onclick to 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/

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:

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.

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. links is in an XPCNativeWrapper.

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

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).

Array.from(document.all).forEach(el=>{
el.onselectstart=null
el.oncontextmenu=null
document.oncontextmenu=null
})

here is the solution Right click and select event bypassed

I am guessing you have either an href or a JavaScript function being called on the onClick for an <a> link.

You can remove either of these by removing the href tag, the onClick event or in this case both of them.

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

        links[i].href='#';
        links[i].onclick = '';
    }
发布评论

评论列表(0)

  1. 暂无评论