Home  >  Article  >  Web Front-end  >  Detailed explanation of javascript bubbling events, mouse events and dom instances

Detailed explanation of javascript bubbling events, mouse events and dom instances

伊谢尔伦
伊谢尔伦Original
2017-07-22 15:46:571283browse

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

----点击这部分,输出:e.target.tagName : P || e.currentTarget.tagName : p

我是目标p

----点击这部分,输出:e.target.tagName : P || e.currentTarget.tagName : p
----点击这部分,输出:e.target.tagName : p || e.currentTarget.tagName : p

//看下第二个变列:

我是目标p

----点击这部分,输出:e.target.tagName : p || e.currentTarget.tagName : p

我是目标p

----点击这部分,输出:e.target.tagName : P || e.currentTarget.tagName : p
----点击这部分,输出:e.target.tagName : p || e.currentTarget.tagName : 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=false
Bubbling 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

use your mouse to click and double click the red square

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn