大多数事件在所有浏览器中都会冒泡。然而,我知道在Internet Explorer中 "submit" 事件不会冒泡。还有哪些事件不会冒泡?
大多数事件在所有浏览器中都会冒泡。然而,我知道在Internet Explorer中 "submit" 事件不会冒泡。还有哪些事件不会冒泡?
load
- 载入事件unload
- 卸载事件scroll
(除非在文档上的滚动事件会冒泡到窗口)- 滚动事件focus
- 获取焦点事件blur
- 失去焦点事件DOMNodeRemovedFromDocument
- 节点从文档中移除事件DOMNodeInsertedIntoDocument
- 节点插入文档事件loadstart
- 开始载入事件progress
- 进度事件error
- 错误事件abort
- 取消事件load
- 载入事件loadend
- 载入结束事件来源: https://en.wikipedia.org/wiki/DOM_events#Events
为了检查一个事件是否通过DOM树向上冒泡,您应该检查 事件对象和其实例上可用的只读bubbles属性 。
"bubbles只读属性指示事件是否通过DOM树向上冒泡。"
在以下代码示例中,您可以检查“focus”事件只能在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...">
任何特定于一个元素的事件都不会冒泡:focus、blur、load、unload、change、reset、scroll、大多数DOM事件(DOMFocusIn、DOMFocusOut、DOMNodeRemoved等)、mouseenter、mouseleave等。
焦点事件和失去焦点事件不会冒泡
mouseover
是否包括在内?此外,你能为这个信息提供引用吗? - Phrogz