如何从内容脚本范围中删除事件?

时间:2014-04-05 21:32:59

标签: javascript jquery javascript-events content-script

我试图从某个特定元素中取消绑定事件并在研究时发现this。哪个有用。在自身。我不知道你能做到这一点。但是:

有没有办法让它在浏览器/ Chrome扩展程序中运行?我正在谈论内容脚本。

之所以没有按照它所描述的方式工作的原因是,使用自己的脚本附加了相关事件的网站使用的是与我的扩展中的jQuery对象不同的jQuery对象&# 39; s includes/文件夹。我可以尝试通过jQuery._data(el, 'click');搜索事件,但这是我的 jQuery对象,而不是显然存储事件的网站之一。我很高兴在经过几个小时的摆弄后想出

或者也许可以访问网站的jQuery对象本身?

修改

我最终试图在理论上实现作品但是......它很复杂。原始脚本使用插件事件并继续使用.on('mouseleave',…重新安装。

无论如何,这就是我得到你的感谢,pdoherty926:

var $el = $('div.slideshow');
$('h2', $el).click(function(){  console.log('ouch!');  });  // test event
var $slides = $('.slides', $el).detach();
$copy = $el.clone(false);
$slides.prependTo($copy);
$el.replaceWith($copy);

测试事件没有被触发,但事实上我实际上试图移除仍然触发的事件。不过,我能想象出来,现在我已经接近了目标。

修改

好的,上面重新安装的mouseleave确实搞砸了这个令人满意的建议。 (该网站正在使用jQuery Timer plug-in by Cyntaxtech)。所以我在这里解决了这个问题:我只是更改了班级名称(-.-')
现在,重新安装代码无法再找到该元素。

这就是我完成的脚本的样子:

function stop_happening() {

  var $el = $('div.fullwall div.slideshow');

  $el
     // first, stop the current automation.
    .timer('stop')    // Timer plug-in

     // next, change class name in order to prevent the timer 
     // from being started again.
    .removeClass('slideshow').addClass('slideshow-disabled-automation');

   //--- copied some extra code from the website itself for the onclick 
   // events which are supposed to keep working. I wish I could do *that* 
   // programmatically but I'm glad I got as far as I got. ---//
   // […]        
}

1 个答案:

答案 0 :(得分:0)

好的,前面提到的在mouseleave上的重新安装确实弄乱了这个令人满意的建议。 (该网站正在使用jQuery Timer plug-in by Cyntaxtech)。因此,这是我解决的方法:我只是更改了类名(-.-')
现在,重新安装代码无法再找到该元素。

这是我完成的脚本的样子:

{a: 1} === {a: 1} // returns false