gpt4 book ai didi

javascript - 功能单击不适用于右键单击 li 元素

转载 作者:行者123 更新时间:2023-11-30 14:24:32 25 4
gpt4 key购买 nike

当我右键单击 <a href="#" class="sim-row-edit" data-type="link">Portfolio</a> 时出现右键菜单

右侧菜单出现 3 个选项:

<ul class='custom-menu'>
<li data-action = "first">First thing</li>
<li data-action = "second">Second thing</li>
<li data-action = "third">Third thing</li>
</ul>

当我点击这些 li 元素时,我应该会收到一条我没有收到的消息。

下面是我的代码:

// when we're about to show the context menu, show our own instead
$(document).on("contextmenu", function (event) {
// Avoid the real one if this is the link
if ($(event.target).hasClass("sim-row-edit")) {
event.preventDefault();
// Show contextmenu
$(".custom-menu").show(100).
css({
top: event.pageY + "px",
left: event.pageX + "px"
});
}
});

// hide our context menu when the document is clicked
$(document).on("mousedown", function () {
$(".custom-menu").hide(100);
});

$(".custom-menu li").click(function(){
alert("hii");
// This is the triggered action name
switch($(this).attr("data-action")) {
// A case for each action. Should personalize to your actions
case "first":
console.log("first");
break;
case "second":
console.log("second");
break;
case "third":
console.log("third");
break;
}
});
.custom-menu {
display: none;
z-index:1000;
position: absolute;
background-color:#fff;
border: 1px solid #ddd;
overflow: hidden;
width: 120px;
white-space:nowrap;
font-family: sans-serif;
-webkit-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
-moz-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
}

.custom-menu li {
padding: 5px 10px;
}

.custom-menu li:hover {
background-color: #4679BD;
cursor: pointer;
}
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>

<a href="#" class="sim-row-edit" data-type="link">Portfolio</a>
<ul class='custom-menu'>
<li data-action = "first">First thing</li>
<li data-action = "second">Second thing</li>
<li data-action = "third">Third thing</li>
</ul>

代码$(".custom-menu li").click(function(){...不工作。

最佳答案

您只需使用 mouseup 而不是 mousedown 这样 click 事件就可以在隐藏目标元素之前触发:

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

// when we're about to show the context menu, show our own instead
$(document).on("contextmenu", function(event) {
// Avoid the real one if this is the link
if ($(event.target).hasClass("sim-row-edit")) {
event.preventDefault();
// Show contextmenu
$(".custom-menu").show(100).
css({
top: event.pageY + "px",
left: event.pageX + "px"
});
}
});

// hide our context menu when the document is clicked
$(document).on("mouseup", function() {
$(".custom-menu").hide(100);
});

$(".custom-menu li").click(function() {
alert("hii");
// This is the triggered action name
switch ($(this).attr("data-action")) {
// A case for each action. Should personalize to your actions
case "first":
console.log("first");
break;
case "second":
console.log("second");
break;
case "third":
console.log("third");
break;
}
});
.custom-menu {
display: none;
z-index: 1000;
position: absolute;
background-color: #fff;
border: 1px solid #ddd;
overflow: hidden;
width: 120px;
white-space: nowrap;
font-family: sans-serif;
-webkit-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
-moz-box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
box-shadow: 2px 2px 7px 0px rgba(50, 50, 50, 0.5);
}

.custom-menu li {
padding: 5px 10px;
}

.custom-menu li:hover {
background-color: #4679BD;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="sim-row-edit" data-type="link">Portfolio</a>
<ul class='custom-menu'>
<li data-action="first">First thing</li>
<li data-action="second">Second thing</li>
<li data-action="third">Third thing</li>
</ul>

关于javascript - 功能单击不适用于右键单击 li 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52164989/

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