duanhegn231318
2016-08-29 18:56
浏览 316

Contextmenu右键单击事件

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

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;
  });

图片转代码服务由CSDN问答提供 功能建议

http://jsbin.com/iGaHAtu/2/edit?html,css,js,output \ n

看看这个链接,我想得到点击表的厄运元素。 我该怎么做 ? 我将尝试使用此代码,但不起作用。

例如,当我单击右侧并打开上下文菜单时,我想要列文本。任何人都可以提醒内部列吗?

  $(“body”)。on(“contextmenu”,“table tr”,function(e){
 $ contextMenu.css({
 display:“block”  ,
 left:e.pageX,
 top:e.pageY 
}); 
 console.log($(this)); 
 return false; 
}); 
  <  / pre> 
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongpao1918 2016-08-29 19:03
    已采纳

    Use $(e.target) for jquery object representing clicked element. Otherwise just use e.target for plain old javascript dom element.

    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

    点赞 评论
  • dov11020 2016-08-29 19:05

    First use TR>TD selector

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

    点赞 评论
  • du131642 2016-08-29 19:36

    Internet Explorer introduced element.innerText, other browsers use 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;
        });
    });
    
    点赞 评论

相关推荐 更多相似问题