停止传播的jquery不按预期工作 [英] stopPropagation of jquery not working as expected

查看:117
本文介绍了停止传播的jquery不按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ui刻录机,在那里我用了一段时间来吸收事件,使用jquery的stopPropagation(定义为

  stopPropagation:function(){
this.isPropagationStopped = K;
var a = this.originalEvent;
!a || (a.stopPropagation& a.stopPropagation(),a.cancelBubble =!0)
}

通过jquery。我把这个事件当成了

  absorbClick:function(e){
e.stopPropagation();
e.preventDefault();





并通过函数将这个事件附加到所有的元素上

pre $ j $ c $ j $ jQuery(frame.document).bind(click,{},function(e){
e.stopPropagation();
e.preventDefault();
},true);

令人惊讶的是,对于某些网站,我无法吸收点击事件的网站。它正常工作。



以上过程不适用于此示例网站。即使在调用absorbClick函数之前,文本也会在下面的文字中变化。为什么我不能吸收事件?

 < html> 
< head>
< title> Repro< / title>
< script src =http://code.jquery.com/jquery-1.11.0.min.js>< / script>
< script src =http://code.jquery.com/jquery-migrate-1.2.1.min.js>< / script>
< / head>
< body>
< div id =mydiv>点击我!< / div>
< script>
$('#mydiv')。click(function(){$('#mydiv')。html(您点击过我!)});
< / script>
< / body>
< / html>



 < div class =central-featured-lang lang1lang =en> 
< em>免费百科全书< / em>< br>
< small> 4 479 000+篇< / small>
< / a>
< / div>

所有上述过程都是 https://github.com/sebuilder/se-builder/blob/master/seleniumbuilder /chrome/content/html/js/builder/verifyexplorer.js 。我正在解决这个问题目前为sebuilder。
解决方案

问题是事件处理程序在该示例中执行之前您的事件处理程序被执行,因为它直接绑定到元素。

您的事件处理程序被添加到文档的根目录,所以它将成为任何事件最后一个处理程序。



如果要在触发任何其他处理程序之前捕获事件,则必须在c addEventListener

  document.addEventListener('click',function ){
event.stopPropagation();
event.preventDefault();
},true); //< - 传递true绑定捕获阶段的处理程序

jQuery不允许您执行为了兼容性的原因。不支持 addEventListener 的I​​E浏览器也不支持捕获阶段的绑定。


I a currently working on a ui recorder where I some times absorb the events using stopPropagation of jquery which is defined as

    stopPropagation: function () {
        this.isPropagationStopped = K;
        var a = this.originalEvent;
        !a || (a.stopPropagation && a.stopPropagation(), a.cancelBubble = !0)
    }

By jquery. I eat the event as

    absorbClick: function(e) {
        e.stopPropagation();
        e.preventDefault();
    }

and attach the event to all the elements by the function

  jQuery(frame.document).bind("click", {}, function(e) {
        e.stopPropagation();
        e.preventDefault();
    }, true);

What is surprising is that for some of the websites, I fail to absorb the click events while for some of websites. It works properly.

Above process does not work fort this sample website. Even before the absorClick function is called the text changes in the following one. Why am I not able to absorb event?

<html>
    <head>
        <title>Repro</title>
        <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    </head>
    <body>
        <div id="mydiv">Click me!</div>
        <script>
            $('#mydiv').click(function() { $('#mydiv').html("You have clicked me!") });
        </script>
    </body>
</html>

while it does work for

<div class="central-featured-lang lang1" lang="en">
    <a class="link-box" href="//en.wikipedia.org/" title="English — Wikipedia — The Free Encyclopedia"><strong>English</strong><br>
        <em>The Free Encyclopedia</em><br>
        <small>4 479 000+ articles</small>
    </a>
</div>

All the above process is part of https://github.com/sebuilder/se-builder/blob/master/seleniumbuilder/chrome/content/html/js/builder/verifyexplorer.js. I am fixing this issue present for sebuilder.

解决方案

The problem is that the event handler in the example is executed before your event handler is executed, since it is directly bound to the element.

Your event handler is added to the root of document, so it will be the very last handler that is executed for any event.

If you want to capture events before any other handler is triggered, you have to bind the handler in the capture phase, with addEventListener:

document.addEventListener('click', function(event) {
    event.stopPropagation();
    event.preventDefault();
}, true); // <- passing true binds the handler in the capture phase

jQuery doesn't let you do that for compatibility reasons. IE browsers that don't support addEventListener also don't support binding in the capture phase.

这篇关于停止传播的jquery不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆