我討厭這個由W3C創建的鼠標按鈕混亂!我想知道當我收到mousedown事件時是否按下了鼠標左鍵。檢測鼠標左鍵按
我使用此代碼
// Return true if evt carries left mouse button press
function detectLeftButton(evt) {
// W3C
if (window.event == null) {
return (evt.button == 0)
}
// IE
else {
return (evt.button == 1);
}
}
但是,它不能在Opera和Chrome的工作,因爲它恰巧window.event也存在那裏。
那麼我該怎麼做?我有一些瀏覽器檢測,但我們都知道它不能依賴一些瀏覽器最近做的所有屏蔽。如何檢測鼠標左鍵RELIABLY?
我認爲'網站前兩個搜索結果:quirksmode.org鼠標左鍵press'會幫助你。 – 2010-10-15 16:28:31
我看過了,裏面什麼也沒有,只有鼠標右鍵的腳本 – 2010-10-15 16:34:38
你可能想看看[jQuery](http://stackoverflow.com/questions/1206203/how-to-distinguish-left-mouse-單擊和右鍵與jQuery)解決了這個問題,或者只是使用[jQuery的mousedown](http://api.jquery.com/mousedown/)並查看'event.which'值。 – jball 2010-10-15 16:36:21