admin管理员组文章数量:1401641
I want to get index of selected
class between visible elements in jquery.
<ul>
<li>element 01</li>
<li style="display: none">element 02</li>
<li style="display: none">element 03</li>
<li style="display: none">element 04</li>
<li>element 05</li>
<li>element 06</li>
<li class="selected">element 07</li>
<li style="display: none">element 08</li>
</ul>
I have tried these ways
console.log($('ul li.selected').index());
console.log($('ul li:visible.selected').index());
I want the number 3 for the example above: The index of the .selected
element in the ul
ignoring the elements that aren't visible.
I want to get index of selected
class between visible elements in jquery.
<ul>
<li>element 01</li>
<li style="display: none">element 02</li>
<li style="display: none">element 03</li>
<li style="display: none">element 04</li>
<li>element 05</li>
<li>element 06</li>
<li class="selected">element 07</li>
<li style="display: none">element 08</li>
</ul>
I have tried these ways
console.log($('ul li.selected').index());
console.log($('ul li:visible.selected').index());
I want the number 3 for the example above: The index of the .selected
element in the ul
ignoring the elements that aren't visible.
-
You can determine if your elem has a certain CSS style with
($('li#myId').css('display') == 'none')
, same with class usinghasClass
– Alicia Sykes Commented Jun 2, 2019 at 9:57 - What are you expecting as both of those lines output 6 which is the index of the li containing 'element 07'? – Calum Commented Jun 2, 2019 at 10:03
- It's not clear what you're asking for. Why "between visible elements"? What number are you expecting? 6? 3? – T.J. Crowder Commented Jun 2, 2019 at 10:06
- 1 I expect the number 3 to return @T.J.Crowder – Mahdi Bashirpour Commented Jun 2, 2019 at 10:08
-
1
Sorry, question wasn't totally clear. But what I meant was, presuming you already know how to use
index()
, you can then just usehasClass
/css
with that, should work fine – Alicia Sykes Commented Jun 2, 2019 at 10:10
1 Answer
Reset to default 10You can use index
on the result of selecting the visible elements, passing in the selected element (or a jQuery object containing it). index
will find the index of the element within the jQuery set (the visible elements):
var index = $("ul li:visible").index($("ul li.selected"));
Live Example:
console.log($("ul li:visible").index($("ul li.selected")));
<ul>
<li>element 01</li>
<li style="display: none">element 02</li>
<li style="display: none">element 03</li>
<li style="display: none">element 04</li>
<li>element 05</li>
<li>element 06</li>
<li class="selected">element 07</li>
<li style="display: none">element 08</li>
</ul>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
本文标签: javascriptGet index of visible element in jqueryStack Overflow
版权声明:本文标题:javascript - Get index of visible element in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744235612a2596539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论