[英]How to use jquery index in a ul that include divs?
Using my code I get -1
... I should get 2 instead. 使用我的代码,我得到-1
...我应该得到2。
I need to get the position of an li
with class focus
within ul
without considering the div tags like <div class="clm-XXXX">
我需要获得ul
内具有类focus
的li
的位置,而无需考虑<div class="clm-XXXX">
How should I change my code? 我应该如何更改我的代码? Please note (I cannot change the HTML structure). 请注意(我无法更改HTML结构)。
http://jsfiddle.net/4bJtr/3/ http://jsfiddle.net/4bJtr/3/
<div class="snippet-content">
<ul class="layout-1">
<div class="clm-1">
<li>content 1</li>
</div>
<div class="clm-1">
<li>content 2</li>
<li class="focus">content 3</li>
<li>content 4</li>
<li>content 5</li>
</div>
<div class="clm-3">
<li>content 6</li>
<li>content 7</li>
</div>
</ul>
This isn't good html, but here's your solution: 这不是很好的html,但这是您的解决方案:
http://jsfiddle.net/e7rru/ http://jsfiddle.net/e7rru/
var result = $('.snippet-content li').index($('.focus'));
Your original selector ( '.snippet-content ul > li.focus'
) wasn't selecting anything to begin with, since there are no li
elements directly under ul
elements (even though they should be...), 您最初的选择器( '.snippet-content ul > li.focus'
)没有选择任何开头,因为在ul
元素正下方没有li
元素(即使它们应该是...),
Also, to use index()
, you need to send a parameter specifying which element you want the index of. 另外,要使用index()
,您需要发送一个参数,该参数指定要为其索引的元素。 According to the docs , index('.focus')
should work, but for some reason in this case it isn't. 根据文档 , index('.focus')
应该可以工作,但是由于某种原因,它不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.