2013-10-18 38 views
0

鑑於鏈接文本(例如下面的html中的「Two」),我希望能夠確定集合中關聯鏈接的索引的鏈接。例如,鏈接「Two」應返回1(假設從0開始索引)。使用jQuery根據鏈接文本確定鏈接的索引

<div id='linkCollection'> 
    <ul> 
     <li><a href='linkone.html'>One</a></li> 
     <li><a href='linktwo.html'>Two</a></li> 
     <li><a href='linkthree.html'>Three</a></li> 
    </ul> 
</div> 

我可以選擇使用$('#linkCollection ul li')鏈接集合,但我不明白怎麼「搜索」特定的文本,並返回索引。 (我敢肯定,我失去了一些東西非常基本的。)

回答

0
$('li>a').each(function(){ 
    if($(this).text()=="Two"){ 
    var myid=$(this).index(); 
    } 
}); 
0

你好jalperin'm新來的,我使用谷歌翻譯跟你說話,對於英語不好抱歉..

若要檢索索引:

$('#linkCollection ul li').click(function(){ 
    var index = $('#linkCollection ul li').index(this); 
    alert(index); 
});