소스에서 살펴보면 도움이 :
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()
전화 inArray()
, 숫자를 반환
대신에, 이런 식으로 뭔가를하려고합니다. 더 인수를 제공하지 않는 경우
, 그것을 if
에 나뭇 가지와 효과적으로 this[0]
질문에 <li>
및 this.parent().children()
입니다, 귀하의 경우에는
return jQuery.inArray(this[0], this.parent().children());
를 호출하면 부모 <ul>
의 모든 아이들이다. inArray()
은 해당 위치를 반환합니다.
이 일을 할 수있는 매우 효율적인 방법이 아니고 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/ –