开发者

jquery .next isn't working?

take this simple c开发者_开发百科ode:

<div id="container">

    <div class="box">abc</div>
    <div class="box" id="secondbox">abc</div>
    <div>generic</div>
    <div>generic</div>

</div>

Now I add the class box to let's say the last div generic:

$('#container div:last').addClass('box');

Now if i try to select the next .box with this it doesnt' work:

$('#secondbox').next('.box')

returns .length=0


I presume what you actually mean is #container div:last.

next does not find the next element that matches a selector. It finds the next sibling element. If you supply a selector, it tests the element against that selector. If the test fails, an empty selection (i.e. length == 0) is returned.

You need nextAll:

$('#secondbox').nextAll('.box').first();


You should replace $('#container p:last').addClass('box'); with $('#container div:last').addClass('box');

And as lonesomeday said. You should use nextAll selector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜