jQuery没有将div作为nth-childs过滤?

时间:2012-09-11 09:32:03

标签: jquery

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"><script>

<script>
$(document).ready(function() 
{
    $('.sss').filter(':nth-child(n+2)').hide();
    $('.sss').on('mouseenter', '.question', function() 
    {   
        $(this).next().slideDown(200).siblings('.answer').slideUp(200);
    });
});
</script>
</head>
<div class="sss">
    <div class="question">Question</div>
    <div class="answer">Answer</div>

    <div class="question">Question</div>
    <div class="answer">Answer</div>

    <div class="question">Question</div>
    <div class="answer">Answer</div>

    <div class="question">Question</div>
    <div class="answer">Answer</div>
</div>
Divs没有亲子关系,或者我在这里犯了错误?

2 个答案:

答案 0 :(得分:2)

试试这个:

$('.sss').children().filter(':nth-child(n+2)').hide();

如果您需要所有question div,请尝试:

$('.sss').children(':nth-child(2n)').hide();

$('.sss .question').hide();

并且适用于所有answer

$('.sss').children(':nth-child(2n+1)').hide();

$('.sss .answer').hide();

答案 1 :(得分:0)

以下是不使用过滤器的替代方法:

$('.sss').children(':nth-child(n+2)').hide();