简体   繁体   English

Contextmenu右键单击事件

[英]Contextmenu right click event

http://jsbin.com/iGaHAtu/2/edit?html,css,js,output http://jsbin.com/iGaHAtu/2/edit?html,css,js,输出

Look at this link i want to get clicked table doom element . 看看这个链接,我想获得点击表末日元素。 How can i do it ? 我该怎么做 ? I am going to try this code but doesn't worked. 我将尝试此代码,但是没有用。

For Example i want to column text when i clicked right and openned contextmenu.. Can anyone alert the inner column ? 例如,当我单击鼠标右键并打开上下文菜单时,我想在文本列中输入文字。

$("body").on("contextmenu", "table tr", function(e) {
    $contextMenu.css({
      display: "block",
      left: e.pageX,
      top: e.pageY
    });
    console.log($(this));
    return false;
  });

Use $(e.target) for jquery object representing clicked element. $(e.target)用于表示单击元素的jquery对象。 Otherwise just use e.target for plain old javascript dom element. 否则,只需将e.target用于普通的旧javascript dom元素。

In your case, to alert the column text, try this code: 对于您的情况,要警告列文本,请尝试以下代码:

$(function() {

  var $contextMenu = $("#contextMenu");

  $("body").on("contextmenu", "table tr", function(e) {
    $contextMenu.css({
      display: "block",
      left: e.pageX,
      top: e.pageY
    });
    alert($(e.target).text());
    return false;
  });

  $contextMenu.on("click", "a", function() {
     $contextMenu.hide();
  });

});

More on event.target here: https://developer.mozilla.org/en-US/docs/Web/API/Event/target 有关event.target的更多信息,请访问: https : //developer.mozilla.org/en-US/docs/Web/API/Event/target

首先使用TR> TD选择器

$("table tr >td").on("contextmenu", function(e) { alert($(this).text()); //rest of the statements goes here });

Internet Explorer introduced element.innerText , other browsers use element.textContent . Internet Explorer引入了element.innerText ,其他浏览器使用element.textContent

$(function() {
    $("body").on("contextmenu", "table tr", function(e) {
        var target = e.target.parentElement;
        var text = target.innerText || target.textContent;
        alert(text);
        return false;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何禁用 ContextMenu 而不是 WebView 中的右键单击事件 - How to disable the ContextMenu but not the right click event in a WebView JQuery 右键单击上下文菜单 - JQuery right click contextmenu 在文档上的右键单击(contextmenu)事件上在popupmenu上创建无法正常工作 - Creating on popupmenu on a right-click(contextmenu) event on a document not working as expected 通过添加事件侦听器禁用上下文菜单(右键单击中显示的选项)不起作用 - Disabling contextmenu (the options seen on a right click) by adding event listener doesn't work 使用触发器禁用右键单击上下文菜单以将其启用 - Disabling right click contextmenu with trigger to enable it back Firefox与contextmenu事件同时触发click事件 - Firefox fires click event at the same time with contextmenu event 如果在上下文菜单事件中调用,则输入类型颜色单击事件将失败 - Input type color click event fails if called within contextmenu event 如何从鼠标右键单击的上下文菜单中捕获选择? - How to capture the selection from the contextmenu of mouse right click? 在禁用ContextMenu的情况下右键单击按钮会使按钮看起来卡住 - Right Click on Button with ContextMenu disabled makes button look stuck Javascript 在右键单击时切换默认和自定义上下文菜单 - Javascript toggle default and custom contextmenu on right-click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM