If an event causes both a default action and execution of a event handling script:javascript
<a href='' onclick='code here'>clicke me</a> 會限制性click事件,再執行默認事件html
「How do I prevent the default action of the event?」
If you return false
from the event handling script, the default action (following the link, submitting the form) is prevented. This technique was standardized by Netscape 2 and still works fine.java
若是在自定義的事件句柄裏面加個return false,則默認的時間則不會觸發,類如a標籤,表單提交瀏覽器
js的事件添加有三種方式:ui
1.inline(如上) .net
2.traditional ie: element.onclick = doSomething;code
3.w3c and Microsoft ie:element.addEventListener('click',doSomething,false)orm
針對第三種方式:htm
1.netscape : element.addEventListener事件
2.Microsoft : element.attachEvent
事件的冒泡和捕捉,冒泡適用於全部的Netscape 2瀏覽器
冒泡 {由子-> 父}
捕捉{由父 ->子}
<div id='parents'>
<a class='child' id='child' href='javascript:void(0)' style='background:red'>default click action is prevented</a>
</div>
<script>
function $(id)
{
if(typeof(id)=='string')
{
return document.getElementById(id);
}
else
{
return id;
}
}
$('parents').addEventListener('click',function(){alert('you clicked the parentCode!!')},true); //這個第三個參數若是是true的話,表明適用捕獲,false表明冒泡
$('child').addEventListener('click',function(){alert('you clicked the child!!')},false);