如何在greasemonkey中取消绑定jquery事件处理程序?

问题描述:

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

$("p").unbind('click'); 

在greasemonkey中点击事件不解除绑定。我相信这是由于greasemonkey的安全模型从XPCNativeWrapper的第一行包装关联的事件对象造成的,导致第二行无法“找到”它。但是,我似乎无法找到解决方法。有什么建议么?如何在greasemonkey中取消绑定jquery事件处理程序?

更新:像下面的东西在greasemonkey中工作。所以我仍然认为这是一个XPCNativeWrapper问题,我找不到解决的办法。

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

你是如何加载JQuery的?可能无法正确加载:http://joanpiedra.com/jquery/greasemonkey/

编辑 或编辑jQuery代码如下所述:http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error

+0

我将它直接嵌入到我的greasemonkey脚本中。 – James 2011-03-25 19:06:09

+0

jquery的版本是什么?你应该阅读:http://wiki.greasespot.net/Third-Party_Libraries#jQuery – w35l3y 2011-03-26 23:04:16

+0

1.5.1。虽然我在尝试1.3.2的同时解决了这个问题,并得到了相同的结果。 – James 2011-03-27 15:55:23

这样来做:

window.addEventListener('load', function() 
{ 
    jQuery = unsafeWindow['jQuery']; 
    jQuery(document).unbind("contextmenu"); 
    jQuery(document).unbind("keypress"); 
    jQuery(document).unbind("selectstart"); 
    jQuery(document).unbind("mousedown"); 
}); 

我认为jQuery的单击事件呼吁解除绑定(后添加)功能。这就是为什么下面的代码有效。

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

一些时间在你的函数流逝,直到您单击警报按钮,解除绑定()函数单击确定后的作品。

这是我测试的解决方案:

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

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

如果你不喜欢使用超时或间隔Greasemonkey的脚本,你可以鼠标悬停事件添加到您的p元素删除点击。

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

请注意,当使用键盘提交表单时,mouseover方法不安全。 – Rapti 2015-01-22 23:22:08