JS event那点事

 

If an event causes both a default action and execution of a event handling script:javascript

  1. the event handler script is executed first
  2. the default action takes place afterwards

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

 

  

若是是ie浏览器的话,只在冒泡阶段触发,所以触发顺序是先是最里面的,而后是外层的 若是是火狐浏览器,用的是onclick=function(){} 那么触发也是在冒泡阶段 若是是addEventListener,第三个参数是冒泡阶段,与上面的同样 若是第三个参数是捕获流阶段,先是最外层的元素,而后是最内层的元素
相关文章
相关标签/搜索