您的位置:首页 > 移动开发 > Objective-C

[JavaScript]Event Object in FireFox and IE

2007-05-14 17:53 429 查看
How to get event object in FireFox and IE?
* static event function which worked both IE and FireFox
<body onmousemove="displaycoordNS(event);">
<script>
    function displaycoordNS(oEvent){
        window.document.title=oEvent.clientX+" : "+oEvent.clientY;
    }
</script>
<form>
<div id="div1" >aaaa</div>
</form>
</body>

* dynamic event function which worked both IE and FireFox
<body">
<script>
function Button1_onclick() {
    var oH = document.getElementById("div1");
    eval("oH.onclick = div1_onclick");
}
function Button2_onclick() {
    var oH = document.getElementById("div1");
    eval("oH.onclick = null");
}
function div1_onclick(oEvent) {
    if (oEvent == undefined)
    { // IE
        oEvent = event;
    }
    alert("div1_onclick2:" + oEvent.type);
}
</script>
<form>
<div id="div1" >aaaa</div>
<div id="div2" ><input id="Button1" type="button" value="enable" onclick="return Button1_onclick()" /></div>
<div id="div3" ><input id="Button2" type="button" value="disable" onclick="return Button2_onclick()" /></div>
</form>
</body>

IE:             var oEvent = window.event; //or event

in FireFox, it is more complex, you can use different method:
* use callee method.
<body onmousemove="displaycoordNS();">
<script>
    function displaycoordNS(){
        var oEvent = arguments.callee.caller.arguments[0];
        window.document.title=oEvent.clientX+" : "+oEvent.clientY;
    }
</script>
<form>
<div id="div1" >Firefox event object</div>
</form>
</body>

* use apply method
<body onmousemove="displaycoordNS.apply(this, arguments);">
<script>
    function displaycoordNS(){
        var oEvent = arguments[0];
        window.document.title=oEvent.clientX+" : "+oEvent.clientY;
    }
</script>
<form>
<div id="div1" >Firefox event object</div>
</form>
</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息