why does it return an error?
/
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>
<div class="e">e</div>
</div>
jquery
a=$('.x').children();
alert(a.eq(0).nextUntil(a.eq(3)).length);
why does it return an error?
http://jsfiddle/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>
<div class="e">e</div>
</div>
jquery
a=$('.x').children();
alert(a.eq(0).nextUntil(a.eq(3)).length);
Share
Improve this question
edited Apr 9, 2011 at 18:11
theHack
asked Apr 9, 2011 at 18:05
theHacktheHack
2,0049 gold badges27 silver badges34 bronze badges
2
- browser specific? there is no error in Chrome – Anwar Chandra Commented Apr 9, 2011 at 18:11
- @Anwar Chandra: It's possible that Chrome gives you a different string value for an object, something that doesn't cause a parsing error in jQuery, but it still won't give you something that can be used as a selector, so it won't work properly. – Guffa Commented Apr 9, 2011 at 18:18
4 Answers
Reset to default 3.nextUntil()
takes a selector not an object. Try:
alert( a.eq(0).nextUntil( '.' + a.eq(3).attr('class') ).length );
http://jsfiddle/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/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/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).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745454188a4628408.html
评论列表(0条)