简体   繁体   English

仅显示div的第一个子节点

[英]Only display the first child of a div

I've been struggling with finding a way to only display the first child of my div and hide all the rest. 我一直在努力寻找一种方法来只显示我的div的第一个孩子并隐藏所有其余的孩子。 Can this be done purely in css or do I have to involve some Javascript? 这可以纯粹用css完成,还是我必须涉及一些Javascript?

So, how can I get this to only display first p-tag? 那么,我怎样才能让它只显示第一个p-tag?

<div id="test1">
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
</div>

http://jsfiddle.net/8XYNx/ http://jsfiddle.net/8XYNx/

try this 尝试这个

http://jsfiddle.net/8XYNx/10/ http://jsfiddle.net/8XYNx/10/

CSS CSS

  #test1 p{
    display:none;

}
#test1 p:first-child{
    display:block;
}

A combination of :not and first-child should surely do it: 结合:notfirst-child一定应该这样做:

#test1 p:not(:first-child) {
    display: none;
}

Note that this won't work in IE before version 9. 请注意,这在版本9之前的IE中不起作用。

By using :first-child wrapped in :not() : 通过使用:first-child包装:not()

Fiddle 小提琴

#test1 p:not(:first-child) {
    display:none;
}

Note that first-child isn't supported by IE8 or earlier. 请注意,IE8或更早版本不支持first-child If you want complete cross browser support, you will have to use Javascript. 如果您需要完整的跨浏览器支持,则必须使用Javascript。

A Javascript solution would be: 一个Javascript解决方案是:

Fiddle 小提琴

var div = document.getElementById("test1");
var pList = div.getElementsByTagName("p");

for(var i=0; i<pList.length; i++) {
    if(pList[i] != div.children[0]) {
        pList[i].style.display = "none";
    }
}
#test1 p {
    display: none;
}
#test1 p:first-child {
    display: block;
}

http://www.w3.org/TR/CSS2/selector.html#first-child http://www.w3.org/TR/CSS2/selector.html#first-child

By hiding all the P elements, and then implicitly displaying the first p element: 通过隐藏所有P元素,然后隐式显示第一个p元素:

#test1 p {
    display: none;
}
#test1 p:first-child {
    display: block;
}

DEMO DEMO

If a DOCTYPE is declared, this will work in IE7+. 如果声明了DOCTYPE ,这将在IE7 +中起作用。 Check the browser compatibility here (unlike using :not which will only work in IE9 unless you use IE7.js ) 这里检查浏览器的兼容性(与使用不同:not除非你使用IE7.js,否则只能在IE9中使用)

display: none on every element except the first (1n w/ 2 offset). display: none除了第一个(1n w / 2偏移量)之外的每个元素。

#test1 p:nth-child(1n + 2){
display: none;
}

DEMO DEMO

Try this, should work with IE7+ 试试这个, 应该使用IE7 +

CSS CSS

#test1>p ~ p{
    display: none;
}

On jsfiddle jsfiddle

If you need older support then this should be cross-browser. 如果您需要较旧的支持,那么这应该是跨浏览器。

Javascript 使用Javascript

var test1 = document.getElementById("test1").getElementsByTagName("p"),
    i = 1,
    p = test1[i];

while (p) {
    p.style.display = "none";
    p = test1[i += 1];
}

On jsfiddle jsfiddle

$('#test1 p:not(:first-child)').hide();

演示

看看这个URL,使用psudo元素选择要应用CSS的特定元素http://www.w3schools.com/CSS/css_pseudo_elements.asp

Try 尝试

#test1 p:not(:first-child)
{
    visibility:hidden;
}

In your example you used only p elements, but if there are some other elemens included? 在您的示例中,您只使用了p元素,但是如果还包含其他一些元素? In general, to show only the first element, you can use this: 通常,要仅显示第一个元素,您可以使用:

#test1 :not(:first-child) {
    display: none;
}

Notice this doesn't work in IE<9. 请注意,这在IE <9中不起作用。

A fiddle demo . 一个小提琴演示

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

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