zoukankan      html  css  js  c++  java
  • DOM事件处理

    1.事件处理器(event handler)

       onsubmit,onchange,onfocus,onblur,onmouseover,onmouseout,onmousedown,onmousedown,ontouchstart,ontouchend,ontouchmove,orientationchange......

      形式: eg.为按钮元素添加事件处理:      var btn = getElementById('btn'); 

                         btn.onclick = function()  {

                                  //do something...... 

                                 };

      局限性:对特定类型事件只能设置一个事件处理函数

    2.事件监听器(event listener)

      IE8及以前:attachEvent

      标准DOM:addEventListener

      (1)不传参:

          eg.1.事件处理函数: function dosth (){}

             2.事件监听:var btn = getElementById('btn');

                  btn.addEventListener('click',dosth,false);

      (2)传参(将事件处理函数嵌套在匿名函数中):

          eg. 1.事件处理函数:function dosth ( a , b ){}

            2.事件监听:var btn = getElementById('btn');

                  btn.addEventListener('click',function(){

                                        dosth ( i , j ); 

                                      },false);  //false:不启动捕获模式(为方便理解,应先了解DOM事件传递)

          与事件处理器区别:1.同类型事件可以添加多个事件监听器

                   2.不需要用等号赋值

                   3.需要兼容性处理:    var btn = getElementById('btn');

                                if  (btn.addEventListener)

                              {

                                  btn.addEventListener('click',function(){

                                        dosth ( i , j ); 

                                      },false);

                              }

                              else{

                                  btn.attachEvent('on'+click,function(){

                                        dosth ( i , j ); 

                                      },false);

                              }

                   4.可解除绑定:if  (btn.removeEventListener)

                            {

                                btn.removeEventListener('click',function(){

                                        dosth ( i , j ); 

                                      },false);

                            }

                            else{

                                btn.detachEvent('click',function(){

                                        dosth ( i , j ); 

                                      },false);

                            }

  • 相关阅读:
    [cf1217F]Forced Online Queries Problem
    [cf1215F]Radio Stations
    超级楼梯[HDU2041]
    亲和数[HDU2040]
    三角形[HDU2039]
    今年暑假不AC[HDU2037]
    Counting Squares[HDU1264]
    CodeForces Round 195 Div2
    Square Coins[HDU1398]
    The number of divisors(约数) about Humble Numbers[HDU1492]
  • 原文地址:https://www.cnblogs.com/pada/p/3663339.html
Copyright © 2011-2022 走看看