简体   繁体   English

jquery .next无效?

[英]jquery .next isn't working?

take this simple code: 拿这个简单的代码:

<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: 现在我添加类框,让我们说最后一个div泛型:

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

Now if i try to select the next .box with this it doesnt' work: 现在,如果我尝试用它选择下一个.box它不起作用:

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

returns .length=0 返回.length = 0

I presume what you actually mean is #container div:last . 我认为你的意思是#container div:last

next does not find the next element that matches a selector. next 没有找到与选择器匹配的下一个元素。 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 (ie length == 0 ) is returned. 如果测试失败,则返回空选(即length == 0 )。

You need nextAll : 你需要nextAll

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

You should replace $('#container p:last').addClass('box'); 你应该替换$('#container p:last').addClass('box'); with $('#container div:last').addClass('box'); with $('#container div:last').addClass('box');

And as lonesomeday said. 正如lonesomeday所说。 You should use nextAll selector. 您应该使用nextAll选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM