[英]How to get the id of element when right clicked
I like to know how to get the id of li when i right click over this li using javascript or jquery. 我想知道当我使用javascript或jquery右击这个li时如何获取li的id。
<ul>
<li id="liid" class="collapsable">
<div class="hitarea collapsable-hitarea">
</div>
<span class="folder">Group1.2</span>
</li>
</ul>
I have the right click function. 我有右键单击功能。
$(document).bind("contextmenu", function (e) {
// code to get the id of current li
});
Can any one help me please. 任何人都可以帮助我。
This uses event delegation on document
and only fires if an li
is clicked. 这在
document
上使用事件委托,仅在单击li
触发。
$(document)
.on('contextmenu', 'li', function(e) {
e.preventDefault();
console.log(this.id);
});
Compared to adding a handler on $('ul')
or $('li')
, this will only bind a single handler. 与在
$('ul')
或$('li')
上添加处理程序相比,这只会绑定一个处理程序。
You can use this.. 你可以用这个..
If you want open also Context Menu on right click then use below code: 如果你想在右键单击打开上下文菜单,然后使用下面的代码:
$(function() {
$('ul li').on('contextmenu', function() {
alert(this.id);
});
});
and without Context Menu then use below code: 如果没有上下文菜单,请使用以下代码:
$(function() {
$('ul li').on('contextmenu', function(e) {
e.preventDefault();
alert(this.id);
});
});
Happy New year... 新年快乐...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.