標籤:事件 javascrip doctype tde nodename utf-8 asc tor class
阻止事件冒泡
function stopBubble(e){if(e&&e.stopPropagation){//非 IEe.stopPropagation();}else{//IEwindow.event.cancelBubble=true;}}
阻止預設事件
function stopDefault( e ) {//阻止預設瀏覽器動作(W3C)if ( e && e.preventDefault )e.preventDefault();//IE 中阻止函數器預設動作的方式elsewindow.event.returnValue = false;return false;}
事件委託
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><style> #oul li{ display: block; width: 600px; height: 50px; background-color: rgba(0,0,0,0.3); margin: 20px; }</style><body> <ul id="oul"> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> <script> var ul = document.querySelector("#oul"); ul.onclick = function (ev){ var ev = ev || window.event; var target = ev.target || ev.srcElement; if(target.nodeName.toLowerCase() == "li"){ alert("123") } //alert("123") } </script></body></html>
javaScript小點..