Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery隐藏除第一个a之外的所有a_Jquery - Fatal编程技术网

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

使用jquery隐藏除第一个a之外的所有a,jquery,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 h

可能重复:

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>
................
试试这个

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

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

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

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

假设您的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>
它会起作用的

(或)

如果你想使用CSS,我们可以这样做

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

您是否尝试过使用$('.productimg a')。而不是(':first').hide()@roXon这就是他想要的“我只想在每个产品中显示第一个a”,但dome只显示一个而不是一个two@Adil是的,我显然需要一些睡眠:)不用担心,睡觉前检查我的答案:)@Adil:现在我希望它符合作者的要求。我错过了productImg div标签,你现在可以试试吗?对不起,它仍然不能工作@阿迪尔的答案是对的。是的,你的答案是对的。非常感谢。
$("#divID").find("div").each(function () {
            $(this).find('a').hide().eq(0).show();
        });
.productimg a:not(:first-child)
    {
        display:none;            
    }