用jQuery获取父索引

 

当我点击元素时,我想得到它的父

  • 的索引号。

    我正在尝试创建一个carrousel类型函数,它不需要列表项上的item-n类。

     $(".bullets li a").click(function(){ var myIndex = $(this).parent().index(this); showPic(myIndex); }); 

    谢谢你的帮助。

    TD。

     $(".bullets li a").click(function(){ var myIndex = $(this).parent().prevAll().length; showPic(myIndex); }); 

    注意: prevAll().length将为您提供从零开始的索引; 第一项将是0(零)。

    $(this).parent().index();

    简短又甜蜜

    更正:这不短而且甜美

    从哪里来它应该是这样的:

     $(".bullets li a").click(function(){ var myIndex = $(this).parent().index(); console.log(myIndex); showPic(myIndex); }); 

    我该怎么做

     $(".bullets li a").click(function() { var $li = $(this).parent(); var myIndex = $li.parent().children().index( $li ); alert( myIndex ); }); 
     $(".bullets li a").click(function(){ var myIndex = $(this).parent().index(); console.log(myIndex); showPic(myIndex); }); 
     $(".bullets li a").click(function(){ var me = $(this); var myIndex = $.each($("li", $(this).parent().parent()), function(i, item){ if(item == me){ showPic(i); }); }); 

    这是我使用表创建的解决方案。 你可以看到我如何使用选择器。 我相信它可以更干净,但你在这里:

      $('.showColumn').live('click',function() { var theEl = {{id of the element}}; var setEl = $('th#' + theEl + ''); var index = setEl.index(setEl.parentNode); $('table tr').each(function() { $('th:eq(' + index + ')',this).show(); $('td:eq(' + index + ')',this).show(); }); }); 

    cheers.bo