Javascript 如何在右键单击上获取Div Id
我在一个页面中使用jquery的上下文菜单,但当我单击这个div上的右键时,我无法获取特定div的idJavascript 如何在右键单击上获取Div Id,javascript,jquery,Javascript,Jquery,我在一个页面中使用jquery的上下文菜单,但当我单击这个div上的右键时,我无法获取特定div的id $("#id").click(function() { console.log($(this).attr("id")); }) 在jQuery中,触发事件的对象始终是这个 $('#element').on("contextmenu",function(){ alert(this.id); }); 或 这里有一个例子 HTML:: <div id="buttondiv">
$("#id").click(function() { console.log($(this).attr("id")); })
在jQuery中,触发事件的对象始终是这个
$('#element').on("contextmenu",function(){
alert(this.id);
});
或
这里有一个例子
HTML::
<div id="buttondiv">
<button type="button">Click Me!</button>
</div>
我猜你的意思是“右击div”
这将适用于页面上的任何
:
//bind the event to the document, using the delegate of "div"
$(document).on('mousedown', 'div', function (e) {
//check that the right mouse button was used
if (e.which === 3) {
//log the [id] attribute of the element that was right-clicked on
console.log($(this).attr('id'));
}
});
你的意思是“右键单击div”还是“单击div内的按钮”?我理解的问题是如何获取ID。
$(document).ready(function(){
$("button").on("mousedown", function(e){
if(e.which == 3){
var divId = $(this).parent().attr("id");
console.log(divId);
}
})
});
//bind the event to the document, using the delegate of "div"
$(document).on('mousedown', 'div', function (e) {
//check that the right mouse button was used
if (e.which === 3) {
//log the [id] attribute of the element that was right-clicked on
console.log($(this).attr('id'));
}
});