jQuery index()与所有可见的兄弟姐妹相关 [英] jQuery index() in relation to all visible siblings
问题描述
我想获取与所有可见兄弟相关的元素索引。
I'd like to fetch the index of an element in relation to all visible siblings.
A td.index()
确实将td的索引提取给所有其他td兄弟。
A td.index()
does fetch the index of the td to all other td siblings.
但当某些TD设置为时显示什么:无
。我想在计算索引时排除它们。 td.index(':visible')
似乎无效。
But what when some of those TDs are set to display:none
. I want to exclude them when calculating the index. td.index(':visible')
does not seem to work.
推荐答案
var $td = $("#theTD")
$td.siblings(":visible").andSelf().index($td);
以上内容应该按照您的要求进行。基本上获取要在其中搜索的元素集,然后获取元素的索引。
The above should do what you're asking. Basically get the set of elements you want to search within and then get the index of your element within them.
编辑:截至jquery 1.8 andSelf
已弃用, addBack
应在其位置使用:
As of jquery 1.8 andSelf
has been deprecated and addBack
should be used in its place:
var $td = $("#theTD")
$td.siblings(":visible").addBack().index($td);
这篇关于jQuery index()与所有可见的兄弟姐妹相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!