ソースの見た目ができます::
index: function(elem) {
if (!elem || typeof elem === "string") {
return jQuery.inArray(this[0],
// If it receives a string, the selector is used
// If it receives nothing, the siblings are used
elem ? jQuery(elem) : this.parent().children()); // <-- here
}
// Locate the position of the desired element
return jQuery.inArray(
// If it receives a jQuery object, the first element is used
elem.jquery ? elem[0] : elem, this);
},
あなたが見ることができるように、index()
通話、数を返し
代わりに、このような何かを試してみてください。
あなたはif
に引数、それ枝を与えないし、効果的にあなたのケースで
return jQuery.inArray(this[0], this.parent().children());
を呼び出すと、this[0]
が問題の<li>
あるとthis.parent().children()
は親<ul>
のすべての子です。はそれぞれの位置を返します。
これは物事を行うには、非常に効率的な方法ではないとeach()
コールバックのインデックスパラメータと比較するとindex()
はすべて子供に見えるので、それはまた、異なる結果を生成することができます:これは動作します
<div>
<p>A paragraph</p>
<hr>
<p>Another paragraph</p>
</div>
// this logs 0, 1
$("div p").each(function (i) {
console.log(i)
});
// this logs 0, 2 (!)
$("div p").each(
console.log($(this).index());
});
、あなたはちょうどバイブルにjqueryを追加するのを忘れました。 http://jsfiddle.net/St4D7/1/ –