点击隐藏子元素

时间:2012-11-26 20:47:41

标签: javascript jquery

我试图在点击“thead”时隐藏“arrow_o”,但它无效。 jquerys“孩子”不能在桌子上工作吗?

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

3 个答案:

答案 0 :(得分:5)

  

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

所以这个:

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

答案 1 :(得分:2)

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

完整代码:

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

答案 2 :(得分:2)

  

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

jQuery children

改为使用.find()