簡體   English   中英

獲取所選元素的位置,而不是DOM中元素的索引

[英]Get position of selected elements, not index of element in DOM

<div>
  <h3>Heading one</h3>
  <h3>Heading two</h3>
  <h4>heading caption</h4>
  <h3>Heading three</h3>
</div>​

我想在jQuery選擇中獲取元素的位置,而不是它在DOM中的索引。

此刻,如果我alert($("h3").eq(2).index()); 我會'3' - 我想'2'。

http://jsfiddle.net/4QmEF/

傳遞index()一個選擇器:

alert($("h3").eq(2).index("h3"));
<div>
   <h3>Heading one</h3>
   <h3>Heading two</h3>
   <h4>heading caption</h4>
   <h3>Heading three</h3>
</div>

<script type="text/javascript">
$(document).ready(function(){ 
    $("div").children().each(function(){  
        $(this).click(function(){
            alert($(this).context.innerHTML);
        });  
    });
});
</script>`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM