繁体   English   中英

单击时隐藏子元素

[英]hide child element on click

我试图在单击“ thead”时隐藏“ arrow_o”,但是它不起作用。 jQuery“子级”在表中不起作用吗?

html

<thead class="thead">
  <tr>
    <th>
      <div class="arrow_o"></div>
    </th>
  </tr>
</thead>

js

$(document).ready(function() {
  $('.thead').click(function() {
    $(this).children('.arrow_o').hide();
  });
});

.children()方法与.find()的不同之处在于,.children()仅沿DOM树向下移动一个级别,而.find()可以向下遍历多个级别以选择后代元素(孙子等)。

所以这:

$(document).ready(function() {
  $('.thead').click(function() {
    $(this).find('.arrow_o').hide();
  });
});

尝试$('.arrow_o', this).hide(); 这基本上设置了应该放置arrow_o的上下文。

完整代码:

$(document).ready(function() {
  $('.thead').click(function() {
     $('.arrow_o', this).hide();
  });
});

.children()方法与.find()的不同之处在于,.children()仅沿DOM树向下移动一个级别,而.find()可以向下遍历多个级别以选择后代元素(孙子等)。

jQuery儿童

使用.find()代替。

暂无
暂无

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

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