我有html代码:
<div id="all">
<div id="1">
<div id="1-1">
</div>
<div id="1-2">
</div>
</div>
<div id="2">
<div id="2-1">
</div>
</div>
</div>
我的JQuery代码中有一处:
var obj = jQuery("#1-2");
我想检查他父母的哪个孩子(相对于他的父母)是obj(在这种情况下,它应该为第二个索引返回1)。
我尝试过(没有结果):
alert(obj.index());
答案 0 :(得分:6)
您的代码返回正确的索引。我认为你只需要在jquery ready中实现代码。
$(function() {
var obj = jQuery("#1-2");
alert( obj.index() );
});
<强> DEMO 强>
答案 1 :(得分:0)
答案 2 :(得分:0)
试试这个
var $obj = $("#1-2");
// Need to check th index for this structure
var $parent = $('div > div > div');
// You want to find the index of your selector
// based on the selector that follows the above structure
console.log($($parent).index($obj))
<强> Check Fiddle 强>