搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

Learn More

event.button

more options

why does this not work in FF <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script> function mouseButton(e) { alert(event.button); } </script> <input name="Button" type="radio" value="1" onmousedown="mouseButton(this);">

why does this not work in FF <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script> function mouseButton(e) { alert(event.button); } </script> </head> <body> <input name="Button" type="radio" value="1" onmousedown="mouseButton(this);" /> </body> </html>

所有回覆 (2)

more options

The short answer is, "because the event object is not defined." Unlike IE, Firefox doesn't attach events to the global window object. Instead, it is passed to the function. You could try something like this:


<!DOCTYPE html> <html> <head> <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"> <script type="text/javascript"> function mouseButton(e){ if (!e) e = window.event; var tgt = (e.target) ? e.target : e.srcElement; if (e.which){ // not IE switch (e.button){ case 0: var btn = "left"; break; case 1: var btn = "middle"; break; case 2: var btn = "right"; } } else { // IE switch (e.button){ case 1: var btn = "left"; break; case 4: var btn = "middle"; break; case 2: var btn = "right"; } } alert(btn + " mouse button down on " + tgt.nodeName + "#" + tgt.id); } window.onload = function(){ document.getElementById("rad1").onmousedown = mouseButton; } </script> </head> <body> <p><input type="radio" name="button" value="1" id="rad1"></p> </body> </html>

Note: because different browsers return different values for e.button, I'm not sure this is 100% reliable. You probably can find better code for sorting out the button value.

more options

As you may have guessed what I needed was much more complex than this, but with your post I have been able to do what I needed, thank you very much for your help. All the best Keith