简体   繁体   English

我的 mouseup 代码有什么问题?

[英]Whats wrong with my mouseup code?

Trying to detect left click vs right click (without using jQuery!) and I have the following code尝试检测左键单击与右键单击(不使用 jQuery!),我有以下代码

Javascript: Javascript:

function cclick(e) {
   if (e.button == 1) {alert("hgbdygwea");}
   else {
      alert(e.button);
   }
}

HTML: HTML:

<a onmouseup="cclick()" ...> <img .../> </a> <!-- also tried cclick instead of cclick() -->

using inte.net explorer 9使用 Internet Explorer 9

You need to pass the event object to your function:您需要将事件 object 传递给您的 function:

onmouseup="cclick(event);"

Quirksmode has a good write up on the subject of " Which mouse button has been clicked? " and his code works over the top of yours. Quirksmode 对“单击了哪个鼠标按钮? ”这一主题有一篇很好的文章,他的代码在你的代码之上工作。

Following is another approach以下是另一种方法

function cclick() {
   var e = window.event;
   if (e.button == 1) {alert("hgbdygwea");}
   else {
      alert(e.button);
   }
}

And call the function as below without passing the event .并在不通过event的情况下按以下方式调用 function。

<a onmouseup="cclick()" ...> <img .../> </a>

I think this code should work for you.我认为这段代码应该适合你。 e.button is not valid for all browsers (and I cleaned up the code). e.button 并非对所有浏览器都有效(我清理了代码)。

function cclick(e) {
    "use strict";
    e || window.event;
    if (e.which) {
        alert((e.which === 1) ? "hgbdygwea" : e.which);
    } else {
        alert((e.button === 1) ? "hgbdygwea" : e.button);
    }
}

<a onmouseup="cclick(event)" ...> <img .../> </a> <!-- also tried cclick instead of cclick() -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM