开发者

nextUntil() jQuery selector

why does it return an error?

http://jsfiddle.net/L82JU/

Uncaught TypeError: Object [object Object] has no method 'replace'

I want to select the first child of .x until the 3rd child of .x

html

<div class="x">
    <div class="a">a</div>
    <div class="b">b</div>
    <div class="c">c</div>
    <div class="d">d</div>
 开发者_JAVA百科   <div class="e">e</div>
</div>

jquery

a=$('.x').children();
alert(a.eq(0).nextUntil(a.eq(3)).length);


.nextUntil() takes a selector not an object. Try:

alert( a.eq(0).nextUntil( '.' + a.eq(3).attr('class') ).length );

http://jsfiddle.net/L82JU/3/


$.nextUntil expects a string, not an object. In your example, you're passing an object, which has no replace method. You need to pass the selector.

You could try this instead:

alert(a.eq(0).nextUntil('.d').length);

Or if you don't know the specific selector ahead of time:

alert(a.eq(0).nextAll().slice(2).length);

http://jsfiddle.net/L82JU/5/


The nextUntil method takes a selector, not an element.

Also, you should not use eq(0) before nextUntil, that will reduce the collection to the first element, and you can't loop to the third element in a collection with only one element.

http://jsfiddle.net/L82JU/4/

a=$('.x').children();
alert(a.nextUntil('.c').length);


I would rather write it like this, it's faster and shorter:

$('.x').children(':lt(4)');

This selects all the children of '.x' that is less than 4 (1-3).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜