js 事件冒泡 和其它問題

js事件冒泡
<html>
<head>
<title>
阻止JavaScript事件冒泡傳遞(cancelBubble 、stopPropagation)
</title>
<meta name="keywords" content="JavaScript,事件冒泡,cancelBubble,stopPropagation" />
<script type="text/javascript">

function doSomething (obj,evt) {

alert(obj.id);

var e=(evt)?evt:window.event; //判斷瀏覽器的類型,在基於ie內核的瀏覽器中的使用cancelBubble

if (window.event) {

e.cancelBubble=true;

} else {

//e.preventDefault(); //在基於firefox內核的瀏覽器中支持做法stopPropagation

e.stopPropagation();

}

}

</script>
</head>
<body>
<div id="parent1" onClick="alert(this.id)" style="width:250px;background-color:yellow">
<p>This is parent1 div.</p>
<div id="child1" onClick="alert(this.id)" style="width:200px;background-color:orange">
<p>This is child1.</p>
</div>
<p>This is parent1 div.</p>
</div>
<br/>
<div id="parent2" onClick="alert(this.id)" style="width:250px;background-color:cyan;">
<p>This is parent2 div.</p>
<div id="child2" onClick="doSomething(this,event);" style="width:200px;background-color:lightblue;">
<p>This is child2. Will bubble.</p>
</div>
<p>This is parent2 div.</p>
</div>
</body>
</html>
ie 一個標籤沒有寫正確,整個頁面都顯示不出來~~

js有效利用事件冒泡做1000dom元素的事件加載
遍歷一個id爲domid下的所有dom元素
解析一個json
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章