javascript event兼容性随笔

时间:2021-09-18 16:47:00

一、前言

     function ConvertEvent(e, element) {
var event = e || window.event;
var resultEvent = {
event: event,
type: event.type,
target: event.target || event.srcElement,
relatedTarget: event.relatedTarget || event.fromElement || event.toElement,
clientX: event.clientX,
clientY: event.clientY,
screenX: event.screenX,
screenY: event.screenY,
// offsetX: event.offsetX,
// offsetY: event.offsetY,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
shiftKey: event.shiftKey,
charCode: event.charCode || event.keyCode,
charCodeValue: String.fromCharCode(event.charCode || event.keyCode),
stopPropagation: event.stopPropagation || function () { return window.event.cancelBubble = true; },
preventDefault: event.preventDefault || function () { return window.event.returnValue = false; }
}; return resultEvent;
} var eventClass = window.Event = window.jasen.core.Event;
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>