使用jquery隐藏除第一个之外的所有a

时间:2012-11-02 05:54:29

标签: jquery

  

可能重复:
  jQuery - hide all elements except first one

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();

4 个答案:

答案 0 :(得分:2)

使用gt隐藏除第一个元素以外的所有元素

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

demo

答案 1 :(得分:0)

试试这个,

<强> Live Demo

$('.productimg').each(function(){
    $(this).children().not(':first').hide();     
});​

答案 2 :(得分:0)

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

答案 3 :(得分: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;            
    }
相关问题