jQuery:查找前两个孩子 [英] jQuery: Find first two children
本文介绍了jQuery:查找前两个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用jQuery,如果一个是 h1
而另一个是<$ c,那么最有效的方法是找到父元素的前两个子元素$ c> p 。我的代码现在无法正常工作,我想使用最佳做法完成此操作。
Using jQuery, what would be the most efficient way to find the first two children of a parent element, if one is an h1
and the other is a p
. My code isn't working right now, and I would like to accomplish this using best practices.
CSS
div > *{
display: none;
}
HTML
<div>
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div>
<div>
<h1>Heading</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</div>
Javascript
$('div h1').show();
$('div p:first-child').show();
编辑我实际使用多个DIV。
Edit I'm actually working with multiple DIVs. I didn't think it would make a difference, but it looks like I was wrong.
推荐答案
尝试,
$('div').children().slice(0,2).show();
如果你有更多的div,然后尝试如下,
Incase if you have more that 1 div, then try like below,
$('div').each (function () {
$(this).children().slice(0,2).show();
});
这篇关于jQuery:查找前两个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文