`

javascript - trick to fix the event object

阅读更多

Many browsers, especially Internet Explorer, miss out on a number of the W3C event object properties

and methods. The only reasonable way to work around this is to create a new object that simulates the

browser's native event object, fixing issues on it where appropriate (it's not always possible to modify the existing object, many properties can't be overwritten).

 

The details of what to fix is embedded in the comment itself. please have a read.

 

/**************************************
*@Name: expandoattr.js
*@Summary
*  Many browsers, expecialy Internet explorerr, miss out a number of W3C event object 
* proeprties and methods, The only reasonale way to wrk this is to create a new object that simulate 
* the browse's native event object. fixing issues on it where appropriate (it's not always possible to modify the
* existing object, many properties can't be overwritten).
*
* some uniformly named 
*   .target - the commonly-use property denoting the original source of the event, IE frequently stores this in .srcElement
*   .relatedTarget - relatedTargt comes into the use when it's used on an event in conjunction with another element (such as "mouseover" or "mouseout"). The .toElement and .fromElement are IE's counterparts
*   .preventDefault - dosn't exist in the IE implementaion , which would normally prevetnt the default browser acton from occuring, instead the .returnValue property needs to be set to false
*   .stopPropagation() - alsoes does not exit, which would normally stop the event from bubbling further up the treee, setting the .canceBubble property to true will make this happen
*   .pageX() - page X and pageY does not exist in IE (provide he position of the mouse relative to the while document) but can be easily duplicated using other information (clientX/Y provides the position of the mouse relative to teh window 
*              of the document, and clientTop/Left gives the offset of the document itself - combining these value will give you the final pageX/Y values).
*   .pageY()
*   .which   - .is equivalent to the key code pressed duing a keyboard event, this cn be duplicated by accessing the .charCode and the .keyCode properties
*   .button - matches the mouse button clicked by the user on a mouse event. Internet explorer uses a bitmask (1 for left client, 2 for right click for middle click) so it needs to be converted to their equivalent values (1, 2,3 )
*   .
* @todo:
*   Test
***************************************/

function fixEvent(event) {
  
  if (!event || !event.stopPropagation) {
    var old = event || window.event;
    // Clone the old object so that we can modify the values
    event = {};
    for (var prop in old) {
      event[prop] = old[prop];
    }
    // The event occurred on this element
    if (!event.target) {
      event.target = event.srcElement || document;
    }
    // Handle which other element the event is related to
    event.relatedTarget = event.fromElement === event.target ?
        event.toElement :
        event.fromElement;
    // Stop the default browser action
    event.preventDefault = function () {
      event.returnValue = false;
      event.isDefaultPrevented = returnTrue;
    };
    event.isDefaultPrevented = returnFalse;
    // Stop the event from bubbling
    event.stopPropagation = function () {
      event.cancelBubble = true;
      event.isPropagationStopped = returnTrue;
    };
    event.isPropagationStopped = returnFalse;
    // Stop the event from bubbling and executing other handlers
    event.stopImmediatePropagation = function () {
      this.isImmediatePropagationStopped = returnTrue;
      this.stopPropagation();
    };
    event.isImmediatePropagationStopped = returnFalse;
    // Handle mouse position
    if (event.clientX != null) {
      var doc = document.documentElement, body = document.body;
      event.pageX = event.clientX +
        (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
        (doc && doc.clientLeft || body && body.clientLeft || 0);
      event.pageY = event.clientY +
        (doc && doc.scrollTop || body && body.scrollTop || 0) -
        (doc && doc.clientTop || body && body.clientTop || 0);

    }
    // Handle key presses
    event.which = event.charCode || event.keyCode;
    // Fix button for mouse clicks:
    // 0 == left; 1 == middle; 2 == right
    if (event.button != null) {
      event.button = (event.button & 1 ? 0 :
        (event.button & 4 ? 1 :
        (event.button & 2 ? 2 : 0)));
    }
  }
  return event;
  function returnTrue() { return true; }
  function returnFalse() { return false; }
}
 

 

 

分享到:
评论

相关推荐

    python-trick

    Python-trick,上传的事pdf文档

    S-a-D-trick.rar_Join In

    split file and join write in vb6

    深度学习领域CNN橄榄球比赛NFL目标检测(带数据集)-cnn-baseline-more-tta-trick

    语言:python 内容包括:源码、数据集、数据集描述、论文 目的:使用CNN算法在橄榄球比赛中目标检测。 带数据集很好运行,主页有搭建环境过程。主页有更多源码。 数据集描述如下: 在这场比赛中,你的任务是预测球员...

    tips-n-trick

    概述 这是Tips-N-Trick,书签等面向公众的文档。

    gtg-grind-trick-generator

    gtg-grind-trick-generator PWA Web应用程序(Node.js,JS,HTML,CSS) Chrome,Safari,Firefox,Edge(Android,iOS,MacOS,Windows) 离线工作Android应用程式使用Google Workbox,Webpack制作

    chinese-nlp-ner-master_chinese_BLSTM_

    一套针对中文实体识别的BLSTM-CRF解决方案,主要包括:数据处理,模型构建,模型训练,模型测试,服务部署(thrift和flask)两种方式。

    py-trick-book:关于 Python 的高级提示和技巧

    Python 提示和技巧 ... git clone https://github.com/plasmashadow/py-trick-book.git 我假设您安装了 ipython 导航到目录并执行 ipython notebook 笔记: 退出间谍活动并自己阅读 或者 保持冷静,加入草帽海贼团

    21-card-trick:在 React 中完成的 21 张卡片技巧

    React 21 卡技巧一个演示卡片技巧的React应用程序。动机该项目旨在学习如何使用 React 钩子和进行嵌套的 api 调用。 该项目不再进行。怎么玩记住 21 张卡片中的 1 张后,单击完成。 选择您的卡片所在的 3 堆中的哪一...

    The Kernel Trick.pdf

    机器学习中为什么需要 Kernel Trick 以及 Kernel Trick 的证明。

    HLP-Trick-crx插件

    语言:English (United States) 该扩展程序重新启用了网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。

    CMCC-Trick:招惹CMCC-* WLAN

    Trick-CMCC 利用CMCC公共热点的小漏洞免费上网~~ :) sudo ./conn.sh Notice: 目前只知道我工CMCC有这特色, 其他地区尚不明确 Notice: 脚本适用于使用NetWorkManager网络sds管理工具的系统 Notice: 不必惊讶原理, ...

    superobject1.5.rar

    * + Now you can force to parse only the canonical syntax of JSON using the stric parameter * + Delphi 2010 RTTI marshalling * v1.1 * + Double licence MPL or LGPL. * + Delphi 2009 compatibility & ...

    HLP-绝招「HLP-Trick」-crx插件

    该扩展程序重新启用网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。 支持语言:English (United States)

    Trick

    Trick

    [Head.First.JavaScript].Michael.Morrison...

    Use the [removed] tag to tell the browser you’re writing JavaScript 11 Your web browser can handle HTML, CSS, AND JavaScript 12 Man’s virtual best friend... needs YOUR help 15 Making iRock ...

    计算机网络第六版答案

    14. If the two ISPs do not peer with each other, then when they send traffic to each other they have to send the traffic through a provider ISP (intermediary), to which they have to pay for carrying ...

    Hat_Trick_The Catch Game.unitypackage

    Hat Trick是Unity官方商店提供的5.5.0版本以上的一款游戏Demo。

    The Manual of Mathematical Magic(数学魔术手册)

    language we use to describe the world around us - it’s the basis of all the sciences. This book will show you how to perform some magical miracles to impress and entertain your friends. But it ...

    Pro Silverlight2 in CSharp 2008

    If this all sounds eerily familiar, it’s because the same trick has been tried before. Several other technologies use a plug-in to stretch the bounds of the browser, including Java, ActiveX, ...

    Pro Silverlight4 in C#.pdf

    If this all sounds eerily familiar, it’s because the same trick has been tried before. Several other technologies use a plug-in to stretch the bounds of the browser, including Java, ActiveX, ...

Global site tag (gtag.js) - Google Analytics