2011-10-22 31 views
2

使用JQuery我們如何找到當前的第n個子元素被點擊的元素是,並提醒該數字。JQuery找到第n個孩子並顯示

$('.bouncetabs a').click(function(){ 
     alert($(this + ':nth-child')) 
     return false 
    }); 

很明顯,這是行不通的。我們該怎麼做呢?

+0

可能重複[jQuery的 - 獲得一個元素的索引與某一類](http://stackoverflow.com/questions/3204349/jquery-get -a-a-index-of-a-class-class) – JJJ

回答

4

我想你想的.index method:中

var selector = '.bouncetabs a'; 

$(selector).click(function(){ 
    //just .index() may work too, depending on the html 
    alert($(this).index(selector)); 
    return false; 
}); 
+0

謝謝呀! .... –