How do I unbind jquery event handlers in greasemonkey?

谁说胖子不能爱 提交于 2019-12-06 12:18:29

I think jQuery click events are added after calling unbind() function. This is why the following code works.

$("p").click(function(){alert('clicked'); $(this).unbind('click')});

Some time elapses in your function until you click the alert button and unbind() function works after you click OK.

Here is a solution I have tested:

function removeClick() {
    $("p").unbind('click');
}

var initTimeout = setTimeout(function() { removeClick(); }, 1000);

If you don't like using timeouts or intervals in a greasemonkey script, you can add mouseover event to your p elements to remove the click.

$("p").mouseover(function(){
    $(this).unbind('click');
});

How are you loading the JQuery? May not be loaded properly: http://joanpiedra.com/jquery/greasemonkey/

EDIT Or edit the JQuery code as described here: http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error

Do it this way:

window.addEventListener('load', function ()
{
    jQuery = unsafeWindow['jQuery'];
    jQuery(document).unbind("contextmenu");
    jQuery(document).unbind("keypress");
    jQuery(document).unbind("selectstart");
    jQuery(document).unbind("mousedown");
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!