2012-11-02 50 views
1

可能重复:
jQuery - hide all elements except first one隐藏所有一个除了第一个使用jQuery

html代码:

<div class="productimg"><a href="">test</a><a href="">tow</a><a href="">three</a></div> 
<div class="productimg"><a href="">test</a><a href="">hide</a><a href="">show</a></div> 
................ 
现在

,我想只显示第一个a在每个productimg打开页面时。我使用下面的代码。但它会隐藏除第一张图像以外的所有图像。

$('.productimg a').hide() 
$('.productimg a:eq(0)').show(); 
+1

你尝试使用$( 'productimg一个。 ')不是(':第一')隐藏(); –

+0

对不起,它不能工作 – runeveryday01

回答

0

试试这个,

Live Demo

$('.productimg').each(function(){ 
    $(this).children().not(':first').hide();  
});​ 
0
$('.productimg a').each(function(){ 
    $(this).hide().filter(":first-child").show(); 
}); 

+0

我很抱歉,它不能工作 – runeveryday01

+0

我错过了productImg的div标签,你现在可以试试吗? – insomiac

+0

我很抱歉,它仍然无法正常工作。 @Adil的回答是对的。 – runeveryday01

2

使用gt躲不是第一要素

其他
$('.productimg').each(function(){ 
    $(this).find('a:gt(0)').hide(); 
}); 

demo

+0

我很抱歉,它不能工作 – runeveryday01

+0

你看到演示了吗? – YogeshWaran

+0

@roXon这就是他想要的“我只想在每个productimg中显示第一个”,但圆顶只显示一个而不是两个 – Adil

0

假设你的HTML代码是这样的,

<div id="divID"> 
    <div class="productimg"> 
     <a href="">test</a> | <a href="">tow</a> | <a href="">three</a></div> 
    <div class="productimg"> 
     <a href="">test</a> | <a href="">hide</a> | <a href="">show</a></div> 
</div> 

试试这个,

$("#divID").find("div").each(function() { 
      $(this).find('a').hide().eq(0).show(); 
     }); 

它会运行

(或)

如果你想使用CSS手段,我们可以做到这一点像,

.productimg a:not(:first-child) 
    { 
     display:none;    
    } 
+0

是的,你的回答是对的。谢谢 – runeveryday01

+0

@ runeveryday01:欢迎 –

相关问题