JS 事件监听方法

来源:互联网 发布:网站平台优化方案 编辑:程序博客网 时间:2024/04/19 14:23

JS事件监听方法

     ||

     ||

     ||====1.传统监听事件绑定方法

     ||||

     ||        ||

     ||        ||====html元素中直接制定:<input type="button"onclick="eventfun(this)"value="button"/>

     ||        ||

     ||        ||

     ||        ||====html元素属性赋值:dom.onclick=eventfun;   //注意此时eventfun中的this指向的将是其定义函数

     ||                             所处execution context中的this值而非dom元素

     ||        

     ||        

     ||====2.现代监听事件绑定方法

     ||        ||

     ||         ||

     ||        ||====W3C方法:obj.addEventListener(evtype,fn,useCapture);  //evtype是事件类型,不带on前缀,

     ||         ||                                                                                             //useCapture是true,则事件处理函数在捕获阶段被执行
     ||         ||                         obj.removeEventListener(evtype,fn,useCapture);

     ||        ||

     ||        ||

     ||        ||====IE方法:obj.attachEvent(evtype,fn);//evtype是事件类型,带on前缀
     ||                                obj.detachEvent(evtype,fn,);

     ||

     ||

     ||====3.两种结合:functionaddEvent(obj,evtype,fn,useCapture)

     || {

  ||if(obj.addEventListener) 
  ||{
  ||obj.addEventListener(evtype,fn,useCapture);
  ||}
  ||else 
  ||{
  ||obj.attachEvent("on"+evtype,fn);
  ||}
  ||}

    ||

    || 

    ||====4.JQUERY:$("#sidebar h3").bind("click",function()

{
$(this).next().show();
})

type:blur、focus、load、resize、scroll、unload、click、dblclick、mousedown、mouseup、

mousemove、mouseover、mouseout、mouseenter、mouseleaye、change、select、

submit、keydown、keypress、keyup和error等,当然也可以是自定义名称。
data是可选参数:作为event.data属性值传递给事件对象的额外数据对象。

0 0
原创粉丝点击