Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>event</title>
</head>
<body>
    <div id='outer'>
        <span>outer</span>
        <div id='middle'>
            <span>middle</span>
            <div id='inner'>
                <span>inner</span>
            </div>
        </div>
    </div>
</body>
</html>
 
#outer{
  text-align: center;
  width: 400px;
  height: 400px;
  background-color: #ccc;
  margin: 0 auto;
}
#middle{
  width: 250px;
  height: 250px;
  background-color: #f00;
  margin: 0 auto;
}
#inner{
  width: 100px;
  height: 100px;
  background-color: #0f0;
  margin: 0 auto;
}
 
function $(element){
  return document.getElementById(element);
}
function on(element,event_name,handler,use_capture){
  if(addEventListener){
      $(element).addEventListener(event_name,handler,use_capture);
    }
  else{
    $(element).attachEvent('on'+event_name,handler);
  }
}
on("outer","click",o_click_c,true);
on("middle","click",m_click_c,true);
on("inner","click",i_click_c,true);
on("outer","click",o_click_b,false);
on("middle","click",m_click_b,false);
on("inner","click",i_click_b,false);
function o_click_c(){
    console.log("outer_捕获");
    alert("outer_捕获");
}
function m_click_c(){
    console.log("middle_捕获");
    alert("middle_捕获");
}
function i_click_c(){
    console.log("inner_捕获");
    alert("inner_捕获");
}
function o_click_b(){
    console.log("outer_冒泡");
    alert("outer_冒泡");
}
function m_click_b(){
    console.log("middle_冒泡");
    alert("middle_冒泡");
}
function i_click_b(){
    console.log("inner_冒泡");
    alert("inner_冒泡");
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
AlvinWei1024pro
0viewers