zoukankan      html  css  js  c++  java
  • Javascript 的addEventListener()及attachEvent()区别分析

    Mozilla中:

    addEventListener的使用方式:

    target.addEventListener(type, listener, useCapture);

    target: 文档节点、document、window 或 XMLHttpRequest。
    type: 字符串,事件名称,不含“on”,比如“click”、“mouseover”、“keydown”等。
    listener :实现了 EventListener 接口或者是 JavaScript 中的函数。
    useCapture :是否使用捕捉,一般用 false 。例如:document.getElementById("testText").addEventListener("keydown", function (event) { alert(event.keyCode); }, false);

    IE中:

    target.attachEvent(type, listener);
    target: 文档节点、document、window 或 XMLHttpRequest。
    type: 字符串,事件名称,含“on”,比如“onclick”、“onmouseover”、“onkeydown”等。
    listener :实现了 EventListener 接口或者是 JavaScript 中的函数。 例如:document.getElementById("txt").attachEvent("onclick",function(event){alert(event.keyCode);});

    W3C 及 IE 同时支持移除指定的事件, 用途是移除设定的事件, 格式分别如下:

    W3C格式:

    removeEventListener(event,function,capture/bubble);

    Windows IE的格式如下:

    detachEvent(event,function);

  • 相关阅读:
    简单makeFile编写
    安装vim、简单linux指令
    XXX 不在sudoers文件中 解决方法
    MYSQL 5.7的那些坑
    testlink的那些坑
    MySQL 增删改查基础
    mysql操作之- 忘记root账户密码
    Python第三方库安装
    写在前面
    一、jenkins的使用 之 新建项目
  • 原文地址:https://www.cnblogs.com/Mygirl/p/2044512.html
Copyright © 2011-2022 走看看