gpt4 book ai didi

javascript - 事件多次发生

转载 作者:行者123 更新时间:2023-12-03 01:10:40 25 4
gpt4 key购买 nike

我已经实现了右键单击上下文菜单。在我的菜单上,我有一个按钮,按下它时我会出现 console.log("button is clicked"); 消息。

我面临的问题是,第一次点击时,我收到消息1次。第二次,只需单击一下,我就会收到消息2次。诸如此类...

下面是我的 jquery 代码:

$(document).on("contextmenu", function(event) {
if ($(event.target).hasClass("sim-row-edit")) { // sim-row-edit ended
console.log("right click identified");
.....
// no problem in here
// multiple occurance problem coming here
$(".custom-menu li").click(function() {
switch ($(this).attr("data-action")) {
case "firstcase":
var thisDiv = target_element.closest('div');
console.log("Button is clicked");
break;
case "nextcase":
...

$(document).on("mouseup", function() {
$(".selected").removeClass("selected");
$(".custom-menu").hide(100);
});

html代码:

<ul class='custom-menu'>
<li data-action = "first">Clone</li>
<li data-action = "second">Remove</li>
<li data-action = "third">Edit</li>
</ul>

如何解决这个问题?

最佳答案

尝试这个event.stopPropagation():

$(".custom-menu li").click(function(e) {
e.stopPropagation();
switch ($(this).attr("data-action")) {
case "firstcase":
var thisDiv = target_element.closest('div');
console.log("Button is clicked");
break;

case "nextcase":
...

引用:https://api.jquery.com/event.stoppropagation/

关于javascript - 事件多次发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52220891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com