[英]stopPropagation of jquery not working as expected
我目前正在使用ui記錄器,我有時會使用jquery的stopPropagation吸收事件,jquery定義為
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();
}
並通過該函數將事件附加到所有元素
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">Click me!</div>
<script>
$('#mydiv').click(function() { $('#mydiv').html("You have clicked me!") });
</script>
</body>
</html>
雖然它確實有效
<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>
以上所有過程均為https://github.com/sebuilder/se-builder/blob/master/seleniumbuilder/chrome/content/html/js/builder/verifyexplorer.js的一部分。 我正在為sebuilder解決這個問題。
問題是示例中的事件處理程序在執行事件處理程序之前執行,因為它直接綁定到元素。
您的事件處理程序被添加到文檔的根目錄中,因此它將是為任何事件執行的最后一個處理程序。
如果要在觸發任何其他處理程序之前捕獲事件,則必須使用addEventListener
在c apture階段綁定處理程序:
document.addEventListener('click', function(event) {
event.stopPropagation();
event.preventDefault();
}, true); // <- passing true binds the handler in the capture phase
出於兼容性原因,jQuery不允許您這樣做。 不支持addEventListener
IE瀏覽器也不支持捕獲階段的綁定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.