zoukankan      html  css  js  c++  java
  • js 事件侦听

    方法

    FireFox : addEventListener()方法

    IE : attachEvent()方法

     

    Code
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     
    <title>Javascript事件监听</title>
    </head>
    <body>
    <button id="Button1">测试</button>
    <script type="text/javascript">
    function addEventHandler(target, type, func) {
        
    if (target.addEventListener)
            target.addEventListener(type, func, 
    false);
        
    else if (target.attachEvent)
            target.attachEvent(
    "on" + type, func);
        
    else target["on" + type] = func;
    }
    function removeEventHandler(target, type, func) {
        
    if (target.removeEventListener)
            target.removeEventListener(type, func, 
    false);
        
    else if (target.detachEvent)
            target.detachEvent(
    "on" + type, func);
        
    else delete target["on" + type];
    }
    var Button1 = document.getElementById("Button1");
    var Button1Click = function() { alert(1); };
    addEventHandler(Button1, 
    "click",  Button1Click);
    addEventHandler(Button1, 
    "click"function() { alert(2); } );
    addEventHandler(Button1, 
    "click"function() { alert(3); } );
    removeEventHandler(Button1, 
    "click"function() { alert(2); } );
    removeEventHandler(Button1, 
    "click", Button1Click);
    </script>
    </body>
    </html>

    HTML元素添加一个事件监听, 而不是直接对元素的事件属性(如:onclickonmouseover)赋值。

    这两种方法处理事件还是有很大区别的!事件属性只能赋值一种方法,即:

    button1.onclick = function() { alert(1); };
    button1.onclick = function() { alert(2); };
    这样后面的赋值语句就将前面的onclick属性覆盖了。

    而添加事件监听就可以并行。

    特别是当团队合作时,事件并行的需求增多,比如:监听document对象的鼠标事件或者window对象的载入事件等。
    使用事件属性则很容易造成事件覆盖掉。

    经过测试IE(8)中先显示3再显示2,而firefox(3)中则先显示2再显示3,这个是为什么呢?测试的结果即真理,没啥好想

    Code
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        
    <title>Javascript事件监听示例</title>
    </head>
    <body>
    <button id="Button1">测试</button>
    <script type="text/javascript">
    /// <summary>
    //
    / 添加事件监听
    //
    / </summary>
    //
    / <param name="target">载体</param>
    //
    / <param name="type">事件类型</param>
    //
    / <param name="func">事件函数</param>
    function addEventHandler(target, type, func) {
        
    if (target.addEventListener)
            target.addEventListener(type, func, 
    false);
        
    else if (target.attachEvent)
            target.attachEvent(
    "on" + type, func);
        
    else target["on" + type] = func;
    }
    /// <summary>
    //
    / 移除事件监听
    //
    / </summary>
    //
    / <param name="target">载体</param>
    //
    / <param name="type">事件类型</param>
    //
    / <param name="func">事件函数</param>
    function removeEventHandler(target, type, func) {
        
    if (target.removeEventListener)
            target.removeEventListener(type, func, 
    false);
        
    else if (target.detachEvent)
            target.detachEvent(
    "on" + type, func);
        
    else delete target["on" + type];
    }
    var Button1 = document.getElementById("Button1");
    var Button1Click = function() { alert(1); };
    addEventHandler(Button1, 
    "click",  Button1Click);
    addEventHandler(Button1, 
    "click"function() { alert(2); } );
    addEventHandler(Button1, 
    "click"function() { alert(3); } );
    removeEventHandler(Button1, 
    "click"function() { alert(2); } ); // 移不出
    removeEventHandler(Button1, "click", Button1Click); // 可以移除
    </script>
    </body>
    </html>
  • 相关阅读:
    Android App 注射&&Drozer Use
    Burp Suite使用介绍总结
    php截取后台登陆密码的代码
    通用型正方教务(通杀各版本)存在注入(不需登陆)+获得webshell+提权内网漫游
    密码重置漏洞案例
    新闻发布系统<分页>
    通过端口 8080 连接到主机 localhost 的 TCP/IP 连接失败
    九大内置对象
    jsp前3章试题分析
    富文本编辑器
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1433005.html
Copyright © 2011-2022 走看看