• javascript event兼容性随笔


    一、前言

     1     function ConvertEvent(e, element) {
     2         var event = e || window.event;
     3         var resultEvent = {
     4             event: event,
     5             type: event.type,
     6             target: event.target || event.srcElement,
     7             relatedTarget: event.relatedTarget || event.fromElement || event.toElement,
     8             clientX: event.clientX,
     9             clientY: event.clientY,
    10             screenX: event.screenX,
    11             screenY: event.screenY,
    12 //            offsetX: event.offsetX,
    13 //            offsetY: event.offsetY,
    14             altKey: event.altKey,
    15             ctrlKey: event.ctrlKey,
    16             shiftKey: event.shiftKey,
    17             charCode: event.charCode || event.keyCode,
    18             charCodeValue: String.fromCharCode(event.charCode || event.keyCode),
    19             stopPropagation: event.stopPropagation || function () { return window.event.cancelBubble = true; },
    20             preventDefault: event.preventDefault || function () { return window.event.returnValue = false; }
    21         };
    22 
    23         return resultEvent;
    24     }
    25 
    26     var eventClass = window.Event = window.jasen.core.Event;
    27     eventClass.Convert = ConvertEvent;

     二、范例

     <form id="form1" runat="server">
        <div>
           <input type="text" value="mouse test" onkeydown="return convert(event);" />
            <input type="button" value="mouse test" onclick="return convert(event,this);" />
        </div>
        </form>
        <script type="text/javascript">
            function convert(e) {
                var eventResult = Event.Convert(e);
                var result = "";
                for (var p in eventResult) {
                    result += (p.toString() + "		 --->" + (eventResult[p] == null ? "null" : eventResult[p]).toString() + "
    ");
                }
    
                alert(result);
            }
        </script>
  • 相关阅读:
    vs2010 在函数级别设置优化
    从快速排序开始的代码演化
    pocket API学习笔记
    工作笔记:复制文件--从windows到ubuntu,再到fedora
    Win10 64位 Apache 2.4+MySQL 5.7+PHP 7环境搭建
    Json简介
    TCP/IP 协议详解和运作过程
    PHP 编程中 10 个最常见的错误,你犯过几个?
    php中的扩展解析
    php.ini设置详解
  • 原文地址:https://www.cnblogs.com/jasenkin/p/javascript_event.html
Copyright © 2020-2023  润新知