在jQuery中识别被点击的目标的子元素 [英] Indentifying a child element of a clicked target in jQuery

查看:46
本文介绍了在jQuery中识别被点击的目标的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to work out how I can right-click anywhere in the table, which has an event listener on the tr tag, and will pass the click() command down to the child button.

jQuery(".catcher").contextmenu(function (e) {
    e.target.**FIND CHILD BUTTON**.click();
});

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>

推荐答案

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

You need to use jquery find method wich searches for first child instance at any depth

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>

这篇关于在jQuery中识别被点击的目标的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆