javascript - 获取点击项目的文本
问题描述
当我右键单击文本时,它会弹出一个菜单,菜单有 option Get text
, Get id
. 我想获取所选链接的文本、id、链接值。
下面是我的代码:
// 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(this);
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" id="sim-id" data-type="link">Portfolio</a>
<ul class='custom-menu'>
<li data-action="first">Get text,id,url of selected</li>
<li data-action="second">Second thing</li>
<li data-action="third">Third thing</li>
</ul>
当您单击“获取所选文本、id、url”时,我需要控制台记录文本、href 值和它的 id。
解决方案
稍微改变你的选择器,像这样:
// 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($(this).parent().parent().children('a').text());
console.log($(this).parent().parent().children('a').attr('id'));
console.log($(this).parent().parent().children('a').attr('href'));
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" id="sim-id" data-type="link">Portfolio</a>
<ul class='custom-menu'>
<li data-action="first">Get text,id,url of selected</li>
<li data-action="second">Second thing</li>
<li data-action="third">Third thing</li>
</ul>
推荐阅读
- javascript - 尝试从网页向网络服务器发送文件
- php - 将 conda env 与 exec 一起使用(www-data 用户)
- javascript - 循环遍历一个数组
并且每次使用 javascript 只显示一个 - unity3d - HoloLens 2 上的空间网格大小限制
- docker - 如何让 Docker 将所有数据存储在 D 盘上?
- flutter - 红色错误屏幕的瞬间闪烁 - Getter UID Called on NULL
- informatica - 如何在 Informatica Cloud Services 中将平面文件 DateTime 转换为 Timestamp 格式
- linux - Linux MCP2551 CAN Bus:一帧后停止工作
- php - Shopware 5 - 如何将数据从订阅者方法传递到 ExtJS
- php - 禁用自动完成电话计费 Woocommerce