开发者

eq() method question

For example, I have this HTML structure:

<ul>
    <li><a class="ac_list_a">some text</a></li>
    <li><a cla开发者_如何学Goss="ac_list_a">some text</a></li>
    <li><a class="ac_list_a">some text</a></li>
    <li><a class="ac_list_a ac_current_a">some text</a></li>
    <li><a class="ac_list_a">some text</a></li>
    <li><a class="ac_list_a">some text</a></li>
</ul>

I need to get the next or previous ac_list_a from ac_current_a.

I have tried to do like this:

var all_a = $('.ac_list_a,.ac_current_a').eq('.ac_current_a').next();

But it doesn't work.

How would you solve it?

Update

That is, of course I can do it the dumb way, by assigning class to all li elemnts, then using

parents(class).next().find(a.class)

But is it possible to do it more dynamic way? Because the structure of HTML could change in the future.


I would probably use:

$('.ac_current_a').parent().next().children()

Demo: jsfiddle.net/dL87J/


eq takes an integer index for a function, not a selector. You can use the index function to find that index, however, and then use eq.

var index = $('.ac_list_a,.ac_current_a').index($('.ac_current_a'));
var all_a = $('.ac_list_a,.ac_current_a').eq(index+1);


Remove eq():

$('.ac_list_a .ac_current_a').parent().next();
$('.ac_list_a .ac_current_a').parent().prev();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜