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() + "\t\t --->" + (eventResult[p] == null ? "null" : eventResult[p]).toString() + "\n");
            }

            alert(result);
        }
    </script>

 

posted @ 2013-11-06 23:52  jasen.kin  阅读(...)  评论(...编辑  收藏