2012-11-26 293 views
1

我试图隐藏“箭头”点击“thead”但它不工作。 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(); 
    }); 
}); 
+0

如果您在使用特定的jQuery方法时遇到问题,那么最好的地方就是[该方法的doco](http://api.jquery.com/children/)... – nnnnnn

回答

5

的。儿童()方法从.find不同()在。儿童()只 行进的单一电平向下DOM树而.find( )可以遍历 多层次选择后代元素(孙, 等)以及。

因此,这:

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

非常感谢您:) – user1766306

+0

此外,检查出Vega的答案,$(selector,this)是一个优雅的,如果一个不太可读的方式来获取子元素。 – Kato

+0

@Kato设置jQuery中的上下文本质上是在jQuery中内部翻译为在特定上下文中使用'.find()'方法,所以这仍然是最好的解决方案,因为它更具可读性:) – Bruno

2

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

全码:

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

的。儿童()方法从.find()在。儿童(不同)仅行进单个电平向下DOM树而.find()可以遍历多个层次来选择后代元素(孙辈等)。

jQuery children

使用.find()代替。