I believe anyone who has used jQuery knows the index () method. It is very convenient for you to find the index location of the current element in the element set. How can you obtain it in native JavaScript? This is a problem I found in my article about writing structure/performance/behavior separation tabs (jQ and native JS. today, a friend asked me this question. This function is indeed very practical. I will share with you my implementation methods.
Code:
The Code is as follows:
Function index (current, obj ){
For (var I = 0; I <obj. length; I ++ ){
If (obj [I] = current ){
Return I;
}
}
}
Principle:
Find the elements in the current set that are equal to the current element through a for loop traversal. The lower value of this element is the index value of the current element in the element set.
Optimization:
The Code is as follows:
Function index (current, obj ){
For (var I = 0, length = obj. length; I If (obj [I] = current ){
Return I;
}
}
}