javascript - HTML DOM : Which events do not bubble?

标签 javascript html dom dom-events event-bubbling

大多数事件在所有浏览器中冒泡。但是,我知道在 Internet Explorer 中“提交”事件不会冒泡。还有哪些不冒泡的事件?

最佳答案

HTML 框架/对象

  • 加载
  • 卸载
  • 滚动(除了文档上的滚动事件必须冒泡到窗口)

HTML 表单

  • 专注
  • 模糊

突变

  • DOMNodeRemovedFromDocument
  • DOMNodeInsertedIntoDocument

进度

  • 加载开始
  • 进度
  • 错误
  • 中止
  • 加载
  • loadend

发件人:https://en.wikipedia.org/wiki/DOM_events#Events


为了检查事件是否通过 DOM 树向上冒泡,您应该检查 read-only bubbles Event 对象及其实例上可用的属性。

"The bubbles read-only property of the Event interface indicates whether the event bubbles up through the DOM tree or not."

在下面的代码示例中,您可以检查“focus”事件如何只能在 capturing phase 期间从 DOM 层次结构 (document.body) 中附加的事件监听器跟踪,而不是在冒泡阶段。另一方面,点击事件可以双向捕获(捕获 + 冒泡阶段)。

// Check if the click event bubbles:
document.querySelector("input").addEventListener("click", e =>{
  console.log(`Does the ${e.type} event bubble?`, e.bubbles);
}, { once: true });
// Check if the focus event bubbles:
document.querySelector("input").addEventListener("focus", e =>{
  console.log(`Does the ${e.type} event bubble?`, e.bubbles);
}, { once: true });

// Track focus event during the bubbling phase (at least trying to):
document.body.addEventListener("focus", e =>{
  console.log(`Tracking ${e.type} event during bubbling phase.`);
});

// Track focus event during the capturing phase:
document.body.addEventListener("focus", e =>{
  console.log(`Tracking ${e.type} event during capturing phase.`);
}, { capture: true })

// Track click event during the bubbling phase:
document.body.addEventListener("click", e =>{
  console.log(`Tracking ${e.type} event during bubbling phase.`);
});

// Track click event during the capturing phase:
document.body.addEventListener("click", e =>{
  console.log(`Tracking ${e.type} event during capturing phase.`);
}, { capture: true })
<input placeholder="Focus or click...">

关于javascript - HTML DOM : Which events do not bubble?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5574207/

相关文章:

javascript - Uncaught Error : NOT_FOUND_ERR: DOM Exception 8 on insertBefore

javascript - 使用 JavaScript 添加 &lt;script&gt; 和 <link> 元素的优缺点是什么?

javascript - 如何检测访问者使用的是 HTTP/2 还是 SPDY?

javascript - 如何将对象中的字符串值转换为数字和 bool 值?

javascript - 第一次点击,结果不符合我的预期

php - 从 MySQL 数据库获取数据到 HTML 下拉列表

javascript - the_content() 没有正确显示描述

javascript - jquery - 如何通过 html 正文中的脚本区分用户操作和操作?

html - <body> 标签的高度与 <html> 标签的高度不相关的原因是什么?

java - StAX 和 DOM 使用 XMLEventReader 而不是 XMLStreamReader