IE和Firefox對於捕獲Event的處理是不同的。
IE把Event作為window的一個屬性,通過window.event的方式進行使用;而FIrefox卻是把Event對象作為函數的一個參數來捕獲的。你是不是比較糊塗了?
別著急,看下面這個例子就明白了。
當滑鼠點擊頁面中某個位置時,彈出一個對話方塊,顯示點擊點在螢幕中的座標。
document.onmousedown=mouseDown;
IE中:
function mouseDown(){
var locString="X="+window.event.screenX+" Y="+window.event.ScreenY;
alert(locString);
}
在Firefox中:
function mouseDown(ffEvent){
var locString="X="+ffEvent.screenX+" Y="+ffEvent.ScreenY;
alert(locString);
}
那我們在實際的代碼中該怎樣寫呢?
現提供一種對IE和Firefox的相容模式,用三元運算子就行判斷(對象檢測),代碼如下:
function mouseDown(ffEvent){
var theEvent=ffEvent?ffEvent:window.event;
var locString="X="+theEvent.screenX+" Y="+theEvent.ScreenY;
alert(locString);
}
該應用的完整代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>X/Y Marks the Spot</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function mouseDown(ffEvent){
var theEvent=ffEvent?ffEvent:window.event;
var locString="X="+theEvent.screenX+" Y="+theEvent.ScreenY;
alert(locString);
}
document.onmousedown=mouseDown;
//]]>
</script>
</head>
<body>
</body>
</html>
物流配貨網http://wlphw.com/ QQ線上:471226865