首页 > 代码库 > JavaScript中事件捕获(Event capturing)-------------->由外向内,事件冒泡(Event bubblin)---------->由内向外

JavaScript中事件捕获(Event capturing)-------------->由外向内,事件冒泡(Event bubblin)---------->由内向外

1 addEventListener("click", function() {}, useCapture:false);

 

<!DOCTYPE html><html lang="en"><head></head><body>    <div id="content" class="c1">        <a id="b">hhaha</a>        <a href="http://www.mamicode.com/#!/fdaf">fda</a>        <a href="http://www.mamicode.com/#!/fdaaa">fdf</a>    </div>    <script>        document.getElementById("b").addEventListener("click", function() {            //this.href = http://www.mamicode.com/this.href.replace("?", "#");            console.log("inner");        },true);                window.addEventListener("hashchange", function() {            console.log(location.hash);        }, false);                document.getElementById("content").addEventListener("click", function() {            console.log("outer");        },true);    </script></body></html>

 

-------------->outer

-------------->inner

 

默认是不用捕获的。---------->由内向外

------------->inner

------------->outer

 

 

区别于xmlhttp.send("GET", url, async:true);--------->表示是否异步async

JavaScript中事件捕获(Event capturing)-------------->由外向内,事件冒泡(Event bubblin)---------->由内向外