计算所有div直到dom的结尾与同一个类后的某个类(包含嵌套的div)?

时间:2013-08-15 13:22:44

标签: javascript jquery

好的我有以下dom树:

    <div class="daterow">
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
</div>
<br/>
<div class="daterow">
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="class1">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
</div>
<br/>
<div class="daterow">
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
</div>
<br/>
<div class="daterow">
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
    <div class="date">TEST CONTENT</div>
</div>

我试图在div之后使用class1类来计算所有带有一类日期的div,如何在jquery或vanilla js中进行此操作这是我到目前为止所得到的但是我失败了,小提琴: http://jsfiddle.net/dXGw3/1/

2 个答案:

答案 0 :(得分:4)

我认为这可以满足您的需求:

$('.date').on('click', function () {
    var divs = $('div');
    var $findchckin = $('.class1');
    var index = divs.index($findchckin);
    divs.filter(':gt(' + index + ').date').addClass('aftercheckin');
});

<强> FIDDLE

如果您的.daterow位于真实网页上的容器中,您可能希望将该容器用作divs的上下文,以提高性能。

答案 1 :(得分:0)

这有效: http://jsfiddle.net/dXGw3/9/

$(function() {
    $('.daterow > .class1').each(function() {
        console.log($(this).nextAll('.date').length);
    });
});