2017-01-09 52 views
0

我的論壇上有太多的註銷鏈接。我不想顯示第二和第三,我只想顯示第一個。選擇一個班級的第二個實例並且不顯示任何

class = bbpresslogouturl 

我想:

.bbpressloginurl:nth-last-of-type(2){display:none;} 
.bbpressloginurl:nth-last-of-type(1){display:none;} 

但這並沒有用css工作。有沒有辦法使用jQuery選擇類的第n個?

+0

'$( 'bbpresslogouturl。 ')不是。(':第一')隱藏()' – haxxxton

+0

':第n-最後, **只關心元素類型**。在CSS中沒有':nth-​​class-class'。你將不得不求助於Javascript。 – connexo

+0

使用':nth-​​child()或.eq()'不是'.eq()'索引從0 – guradio

回答

0

如果他們不是 「兄弟/兄弟姐妹」

$('.bbpressloginurl').eq(1).hide(); 

如果是這樣,你可以使用CSS:

.bbpressloginurl:first-child + .bbpressloginurl{ display: none; } 

這將隱藏inmediate同級

.bbpressloginurl:first-child ~ .bbpressloginurl{ display: none; } 

這會隱藏所有的兄弟姐妹。

甚至這樣的:

.bbpressloginurl:not(:first-child) { display: none; } 

這取決於你的標記,你不同意..

__

而且first-of-typelast-of-type不能在類名被使用,只是元素(ul,h2,..)

+0

這個詞是兄弟姐妹,不是兄弟。而且,即使他們是兄弟姐妹,也不意味着他們的第一個是其父母的第一個孩子。即使是這樣,你也不得不使用同級的裁剪器'〜'而不是相鄰的選擇器'+'。 – connexo

+0

我正在編輯我的答案......但〜〜適用於所有的兄弟姐妹...... –

+0

如果他們的HTML結構能夠滿足您的要求,那麼這就是OP想要的,這幾乎不會。 *我的論壇上有太多的註銷鏈接。我不想顯示第二個和第三個,只顯示第一個。* – connexo

1

您可以使用:

<div class="test"> 
    1 
</div> 
<div class="test"> 
    2 
</div> 
<div class="test"> 
    3 
</div> 

和css:

.test { 
    display: none; 
} 

.test:nth-child(1) { 
    display: block !important; 
} 

入住這fiddle

最好的問候!
剋日什托夫·

1

隱藏所有的人,除了先用

$('.bbpresslogouturl').not(':first').hide(); 
相關問題