简体   繁体   English

数不清 <li> 在特定的左侧和右侧 <li> 在一个 <ul>

[英]Count no of <li> in left side and right side of a particular <li> in a <ul>

Is there a way to know how many <li> in the left side and how many <li> at the right side of a specific <li> in a <ul> . 有没有办法知道<li>中特定<li>的左侧有多少<li>和右侧的有多少<li> <ul>

For ex here I want the output that for the specific <li class="active-slide"> 2 <li> at the left side and 1 at the right side. 对于此处的ex,我想要特定<li class="active-slide">左侧的2 <li>和右侧的1的输出。

Is it possible through jquery 是否可以通过jquery

<ul id="slider_donor" class="clearfix">
 <li id="slider_1">
 <li id="slider_2">
 <li id="active-slide" class="active-slide"> 
 <li id="slider_3">
</ul>

Thanks in advance. 提前致谢。

You can use prevAll() : 您可以使用prevAll()

var leftLIs = $('.active-slide').prevAll('li');

to get all the li which are the previous siblings of your .active-slide . 得到所有的li ,它们是.active-slide的先前兄弟姐妹。

As well as nextAll() ; 以及nextAll() ;

var rightLIs = $('.active-slide').nextAll('li');

to get all the li which are the next siblings of your .active-slide . 得到所有的li ,它们是.active-slide的下一个兄弟姐妹。

You can use the nextAll() and prevAll() to find out the next all previous siblings then get the length of those objects 您可以使用nextAll()和prevAll()找出下一个先前的所有同级对象,然后获取这些对象的长度

var $active = $('.active-slide');
var rights = $active.nextAll().length;
var lefts = $active.prevAll().length;

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

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