JavaScript Event 對象

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.