简体   繁体   中英

Contextmenu right click event

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

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

首先使用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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM