<ul class="bullets">
<li><a href="#">item 1</a></li>
<li><a href="#">item 2</a></li>
<li><a href="#">item 3</a></li>
<li><a href="#">item 4</a></li>
<li><a href="#">item 5</a></li>
</ul>
当我点击上<a>
元素,我想获得它的父<li>
的索引号。
我试图创建一个并不需要在列表项项-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