在jQuery

时间:2016-07-29 04:09:34

标签: javascript jquery

我正在尝试找出如何右键单击表中的任何位置,该表在tr标记上有一个事件侦听器,并将click()命令传递给子{ {1}}。

button
jQuery(".catcher").contextmenu(function (e) {
    e.target.**FIND CHILD BUTTON**.click();
});
table {
  border: 1px solid red;
  width: 200px;
  height: 200px;
}

3 个答案:

答案 0 :(得分:3)

你需要使用jquery find方法搜索任何深度的第一个子实例



jQuery(".catcher").contextmenu(function(e) {
  $(this).find('button').click();
  return false;
});

table {
  border: 1px solid red;
  width: 200px;
  height: 200px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="catcher">
    <td>
      <span>
          <button onclick="alert('it works!');">Button</button>
      </span>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

&#13;
&#13;
jQuery(".catcher").contextmenu(function (e) {
    //e.target.**FIND CHILD BUTTON**.click();
  $(e.target).find('button').click()
});
&#13;
table {
  border: 1px solid red;
  width: 200px;
  height: 200px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="catcher">
    <td>
      <span>
          <button onclick="alert('it works!');">Button</button>
      </span>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;

e.target包裹在$()中,然后使用.find('button')

答案 2 :(得分:0)

请参阅此处数量为班级名称

($(e).parent().parent().find(".Qty").val()