JS事件流模型
2021-05-02 08:27
标签:ace -- 超链接 ext cli over otto 结果 syntax (一)JS中的事件流模型 1. 事件冒泡(fasle/不写):当触发一个节点的事件是,会从当前节点开始,依次触发其祖先节点的同类型事件,直到DOM根节点。 4. 阻止事件冒泡: ① IE10之前,e.cancelBubble = true; 5. 阻止默认事件: ① IE10之前:e.returnValue = false; 结果(事件冒泡)(由小到大div3-》div2-》div1): 结果(事件捕获)(由小到大div3-》div2-》div1): JS事件流模型 标签:ace -- 超链接 ext cli over otto 结果 syntax 原文地址:https://www.cnblogs.com/login123/p/12130427.htmlJS事件流模型
2. 事件捕获(true):当初发一个节点的事件时,会从DOM根节点开始,依次触发其祖先节点的同类型事件,直到当前节点自身。
3. 什么时候事件冒泡?什么时候事件捕获?
① 当使用addEventListener绑定事件,第三个参数传为true时表示事件捕获;
② 除此之外的所有事件绑定均为事件冒泡。
② IE10之后,e.stopPropagation();
② IE10之后:e.preventDefault();//css#div1{ width: 300px;;
height: 300px;
}#div2{ width: 200px;
height: 200px;
}#div3{ width: 100px;
height: 100px;
padding: 0px 0px 0px 5px; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-left: 3px solid rgb(108, 226, 108); line-height: 20px; width: 640px; clear: both; outline: 0px !important; border-radius: 0px !important; border-top: 0px !important; border-right: 0px !important; border-bottom: 0px !important; border-image: initial !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important;">#A9A9A9;
}//html"div1">
"div2">
"div3">
div1.addEventListener("click",function(){
console.log("div1 click");
},false);
div2.addEventListener("click",function(){
console.log("div2 click");
},false);
div3.addEventListener("click",function(){ //原来的顺序是:3-->2-->1。
// myParagraphEventHandler(); //截获事件流后,只触发3.但是从2开始依然会冒泡; console.log("div3 click");
},false);

div1.addEventListener("click",function(){
console.log("div1 click");
},true);
div2.addEventListener("click",function(){
console.log("div2 click");
},true);
div3.addEventListener("click",function(){
// myParagraphEventHandler(); //截获事件流后,只触发3.但是从2开始依然会冒泡; console.log("div3 click");
},true);

//依然遵循事件冒泡document.onclick=function(){
console.log("document click")
}//截获事件流阻止事件冒泡function myParagraphEventHandler(e) {
e = e || window.event;
if (e.stopPropagation) {
e.stopPropagation(); //IE10以后
} else {
e.cancelBubble = true; //IE10之前
}
}//截获事件流阻止事件冒泡function myParagraphEventHandler(e) {
e = e || window.event;
if (e.stopPropagation) {
e.stopPropagation(); //IE10以后
} else {
e.cancelBubble = true; //IE10之前
}
}//阻止默认事件function eventHandler(e) {
e = e || window.event;
// 防止默认行为 if (e.preventDefault) {
e.preventDefault(); //IE10之后
} else {
e.returnValue = false; //IE10之前
}
}