About target and currentTarget in bubbling
Target is in the target stage of the event flow; currentTarget is in the capture, target and bubbling stages of the event flow. Only when the event flow is in the target stage, the two directions are the same. When it is in the capturing and bubbling stages, target points to the clicked object and currentTarget points to the parent of the current event.
<p id="outer" style="background:#099"> <p>我是目标p</p> ----点击这部分,输出:e.target.tagName : P || e.currentTarget.tagName : p <p id="inner" style="background:#9C0">我是目标p</p> ----点击这部分,输出:e.target.tagName : P || e.currentTarget.tagName : p <br> ----点击这部分,输出:e.target.tagName : p || e.currentTarget.tagName : p </p> //看下第二个变列: <p id="outer" style="background:#099"> <p>我是目标p</p> ----点击这部分,输出:e.target.tagName : p || e.currentTarget.tagName : p <p id="inner" style="background:#9C0">我是目标p</p> ----点击这部分,输出:e.target.tagName : P || e.currentTarget.tagName : p <br> ----点击这部分,输出:e.target.tagName : p || e.currentTarget.tagName : p </p>
function getObj(id) { return document.getElementById(id); } function addEvent(obj, event, fn) { if(window.attachEvent) { obj.attachEvent("on" + event, fn); } else if(window.addEventListener) { obj.addEventListener(event, fn, false); } } function test(e) { alert("e.target.tagName : " + e.target.tagName + "\n e.currentTarget.tagName : " + e.currentTarget.tagName); } var outer = getObj("outer"); var inner = getObj("inner"); //addEvent(inner, "click", test); addEvent(outer, "click", test);
The difference between IE and DOM
DOM Get the target event.target event.srcElementGet the character code event.charCode event.keyCode
Prevent the default behavior event.prevetDefault() event.returnvalue=falseBubbling event.stopPropagation() event.cancelBubble= true
About blocking the default behavior, for example, if you don’t want the menu to pop up when the user right-clicks the mouse, you can use the blocking default behavior:
document.body.oncontextmenu=function(event) { if(isIE) { var oEvent=window.event; oEvent.returnValue=false; //也可以直接是return false;阻止默认行为 } else { oEvent.preventDefault(); } }
Mouse event
<p>use your mouse to click and double click the red square</p> <p style="width:100px;height:100px;background:red" onmouseover="handleEvent(event)" onmouseout="handleEvent(event)" onmousedown="handleEvent(event)" onmouseup="handleEvent(event)" onclick="handleEvent(event)" ondblclick="handleEvent(event)" id="p1" > </p> <p><textarea id="txt1" rows="5" cols="45"></textarea></p> <!--检测键盘事件--> <p><input type="text" id="textbox" onkeydown="handle(event)" onkeypress="handle(event)" onkeyup="handle(event)" ></p> <p><textarea id="txt2" rows="10" cols="45"></textarea></p>
js file is as follows:
function handleEvent(event) { var oText=document.getElementById('txt1'); oText.value+= "\n"+event.type; oText.value+= "\n target is "+(event.srcElement||event.target).id; oText.value+="\n at ("+event.clientX+","+event.clientY+")in the client"; oText.value+="\n at ("+event.screenX+","+event.screenY+")in the client"; oText.value+="\n button down is"+event.button; var arrKeys=[]; oText.value+="\n relatedTarget is"+event.relatedTarget.tagName; //event.relatedTarget.tagName可以判断鼠标的来源和出处 } function handle(event) { var oText2=document.getElementById('txt2'); oText2.value+="\n"+event.type; var arrKeys=[]; if(event.shiftKey){arrKeys.push("Shift");} if(event.ctrlKey){arrKeys.push("Ctrl");} if(event.altKey){arrKeys.push("Alt");} oText2.value+="\n keydown is "+arrKeys; }
The above is the detailed content of Detailed explanation of javascript bubbling events, mouse events and dom instances. For more information, please follow other related articles on the PHP Chinese website!