Onclick()在javascript中表现得很奇怪 [英] Onclick() behaves weirdly in javascript

查看:79
本文介绍了Onclick()在javascript中表现得很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML上有一个按钮,并且有一个名为OnAddButtonClick()的onClick函数。每当函数被触发时,javascript将检测它是来自左键单击还是右键单击并根据单击提示输出消息。

但有一点奇怪的是第二次单击,切换案例虽然我只点击了一次,但会自动循环两次。然后如果我再次点击它(第3次),开关盒将循环三次并增加循环次数,因为它继续...

以下是我的javascript:

 OnAddButtonClick: function (){
$(' #btnAdd')。click( function (e){
switch (e.which){
case 1
alert(' 左键单击按下');
break ;
case 3
alert(' Right Click Presse d');
break ;
默认
break ;
}
});
},





任何帮助将不胜感激。谢谢。



我的尝试:



1.搜索在线更好地了解元素点击说明但无济于事。

解决方案

' #btnAdd')。点击( function (e){
switch (e.which){
case 1
alert (' 左键单击按下');
中断;
案例 3
alert(' 右键单击按下');
b reak ;
默认
break ;
}
});
},





任何帮助将不胜感激。谢谢。



我的尝试:



1.搜索在网上可以更好地理解元素点击说明但无济于事。


我已经碰到了下面的解决方案并相应地修改了我的javascript。

javascript - 区分onclick函数内的左右键单击 - Stack Overflow [< a href =https://stackoverflow.com/questions/30301618/distinguish-between-left-and-right-click-inside-onclick-functiontarget =_ blanktitle =New Window> ^

Hi, I have a button on HTML and with a onClick function called OnAddButtonClick(). Whenever the function is fired, javascript will detect whether it is coming from left click or right click and prompt out message based on the click.
But one thing weird is for the second click onward, the switch case will get auto loop twice although I just clicked it once. And subsequently if I click on it again(3rd time), the switch case will loop three times and increase the loop count as it goes on...
Below are my javascript:

OnAddButtonClick: function () {
            $('#btnAdd').click(function (e) {
                switch (e.which) {
                    case 1:
                        alert('Left Click Pressed');
                        break;
                    case 3:
                        alert('Right Click Pressed');
                        break;
                    default:
                        break;
                }
            });
        },



Any help would be appreciated. Thank you.

What I have tried:

1. Search online for better understanding on element click explanation but no avail.

解决方案

('#btnAdd').click(function (e) { switch (e.which) { case 1: alert('Left Click Pressed'); break; case 3: alert('Right Click Pressed'); break; default: break; } }); },



Any help would be appreciated. Thank you.

What I have tried:

1. Search online for better understanding on element click explanation but no avail.


I have bumped upon the solution below and modified my javascript accordingly.
javascript - Distinguish between left and right click inside onclick function - Stack Overflow[^]


这篇关于Onclick()在javascript中表现得很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆