1

可能重复:
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();
4

4 回答 4

2

用于gt隐藏除第一个元素之外的所有元素

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

演示

于 2012-11-02T06:09:10.640 回答
0

试试这个,

现场演示

$('.productimg').each(function(){
    $(this).children().not(':first').hide();     
});​
于 2012-11-02T05:56:08.820 回答
0
$('.productimg a').each(function(){
   $(this).hide().filter(":first-child").show();
});

​</p>

于 2012-11-02T05:56:34.533 回答
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;            
    }
于 2012-11-02T06:13:02.747 回答