我在用 firefox,我想判斷鼠標的按鍵。情況是這樣的,我先按下鼠標左鍵并拖動鼠標選擇文字,在左鍵沒有放開的情況下,再點擊右鍵,請問有沒有辦法判斷這情況下是否擊了右鍵?我試了一下,在onmousedown/onmouseup裡,利用 event.button 無法判斷。因為左鍵沒有放開,所以即使按下右鍵,系統也不會產生一個mousedown或mouseup事件。請問有沒有其它辦法?另外,在網上查到,同時按下左右鍵時event.button返回3。但是,迠個”同時按下”是怎樣定義的?先按下左鍵,在不放開的情況下再按右鍵算同時按下嗎?但我試了很多次,即使我同時按下左右鍵,event.button都是0
<head>
<title>sample</title>
<script language="JavaScript">
window.onload=function(){
var div=document.getElementById("div1");
div.getElementsByTagName("table")[0].onclick=function(){
document.getElementById("div2").innerHTML+="Left Click<br>";
}
div.getElementsByTagName("table")[0].oncontextmenu=function(){
document.getElementById("div2").innerHTML+="Right Click<br>";
return false;
}
}
</script>
</head>
<body>
<div id=div1>
<table border=1><tr><td>1111111111111111</td></tr>
<tr><td>2222222222222222</td></tr>
<tr><td>3333333333333333</td></tr></table>
</div>
<div id=div2></div>
</body>
</html>
<body><div id="te" onmousedown="A()">
div
</div></body><script>function A()
{
document.getElementById("te").onmousemove = function (){
document.onmousedown = Todo;
};
document.getElementById("te").onmouseup = function (){
Clear();
};
}function Todo(e)
{
e = e || window.event;
var leftButton = e.srcElement ? 1 : 0; //因为是左右键同时按,得加上左键的值
if (e.button == 2 + leftButton) {alert("按了右键!");}
else {alert("按了非右键!");} Clear();
}function Clear()
{
document.onmousedown = null;
document.getElementById("te").onmousemove =document.getElementById("te").onmouseup = null;
}
</script>
</html>
试了2#的代码了么???就是你的要求,手上没FF,IE下没问题,不行的话把Todo函数改成这样function Todo()
{
e = arguments[0] || window.event;
var leftButton = e.srcElement ? 1 : 0;
var rightButton = 2; //因为是左右键同时按,得加上左键的值
if (e.button == rightButton + leftButton) {alert("同时按了左右键!");}
else {alert("按了非右键!");} Clear();
}