javascript document.addEventListener("deviceready",OnDeviceReady,false); 中的第三个参数 (false) 表示什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17564323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:48:00  来源:igfitidea点击:

What does the third parameter (false) indicate in document.addEventListener("deviceready",OnDeviceReady,false);

javascripteventsjavascript-events

提问by iJade

What does the third parameter (false) indicate in

第三个参数 (false) 表示什么

document.addEventListener("deviceready",OnDeviceReady,false);

Can any one show an example script to show the difference

任何人都可以显示一个示例脚本来显示差异

采纳答案by lifus

It's useCapture:

它是useCapture

If true, useCaptureindicates that the user wishes to initiate capture. After initiating capture, all events of the specified type will be dispatched to the registered listenerbefore being dispatched to any EventTargets beneath it in the DOM tree. Events which are bubbling upward through the tree will not trigger a listener designated to use capture. See DOM Level 3 Eventsfor a detailed explanation.

如果trueuseCapture表示用户希望启动捕获。启动捕获后,指定类型的所有事件将被分派到注册的,listener然后再分派到EventTargetDOM 树中它下面的任何s。通过树向上冒泡的事件不会触发指定使用捕获的侦听器。有关 详细说明,请参阅DOM Level 3 Events

回答by lonesomeday

This is for historical reasons. When the browser event system was first designed, there were two conflicting ways of modelling how it worked. They were called event capture and event bubbling.

这是历史原因。在最初设计浏览器事件系统时,有两种对其工作方式进行建模的相互冲突的方式。它们被称为事件捕获和事件冒泡。

Take for instance, this HTML:

以这个 HTML 为例:

<html>
    <body>
        <a href="#">Content</a>
    </body>
</html>

If an event (e.g. a click) happens on the aelement, should the ancestor elements know? It was widely accepted that they should. But the question was in what orderthey should be notified. The Microsoft and Netscape developers (this should give you an idea of quite howhistorical we're talking!) had differing opinions.

如果一个事件(例如点击)发生在a元素上,祖先元素应该知道吗?人们普遍认为他们应该这样做。但问题是应该按照什么顺序通知他们。Microsoft 和 Netscape 开发人员(这应该让您了解我们所谈论的历史有多么重要!)有不同的意见。

One model was event capture (advocated by the Netscape developers). This notified the htmlelement first and worked its way down the tree:

一种模型是事件捕获(由 Netscape 开发人员提倡)。这html首先通知元素并沿着树向下工作:

  • html
  • body
  • a
  • html
  • body
  • a

The other model was event bubbling (advocated by the Microsoft developers). This notified the target element first, and worked its way up the tree:

另一种模型是事件冒泡(由微软开发人员提倡)。这首先通知目标元素,然后沿着树向上移动:

  • a
  • body
  • html
  • a
  • body
  • html

The eventual compromise was that it should do both.

最终的妥协是它应该两者兼而有之

  • html(capture phase)
  • body(capture phase)
  • a(capture phase)
  • a(bubbling phase)
  • body(bubbing phase)
  • html(bubbling phase)
  • html(捕获阶段)
  • body(捕获阶段)
  • a(捕获阶段)
  • a(冒泡阶段)
  • body(冒泡阶段)
  • html(冒泡阶段)

So the event works its way down the tree and then back up again.

因此,事件沿着树向下运行,然后再次返回。

This is a long-winded way of getting to addEventListener. addEventListenerlistens for both capture phase and bubbling phase events. The third parameter (called useCapturein the specification) allows the programmer to specify which phase they want to use.

这是到达addEventListener. addEventListener侦听捕获阶段和冒泡阶段事件。第三个参数(useCapture在规范中调用)允许程序员指定他们想要使用的阶段。

In modern browsers, this defaults to false. You will probably never come across a circumstance where you want to use the capturing phase, especially as Internet Explorer still doesn't support it. But old browsers need the falseto be explicit, so it is generally provided for backwards-compatibility.

在现代浏览器中,默认为false. 您可能永远不会遇到要使用捕获阶段的情况,尤其是 Internet Explorer 仍然不支持它。但是旧的浏览器需要false明确的,所以它通常是为了向后兼容而提供的。