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);