indexOf() method definition and usage
The indexOf() method returns the first occurrence of a specified string value in the string.
This method will retrieve the string stringObject from beginning to end to see if it contains a substring searchvalue. The location where the search begins is at the fromdex of the string or at the beginning of the string (when fromdex is not specified). If a searchvalue is found, it returns the location where the searchvalue first appears.
The character position in stringObject starts at 0.
If no string is found in the array, return -1.
Get to the point:
The indexof method in js can find the first index value found in the array with a given element, but indexof is not supported in IE8. This article introduces you the solution to ie8 not supporting indexof
How does a browser not support indexof, you can use the following code at the beginning when writing scripts, which allows you to use the indexOf method without local support.
if (!) { = function(searchElement, fromIndex) { var k; if (this == null) { throw new TypeError('"this" is null or not defined'); } var O = Object(this); var len = >>> 0; if (len === 0) { return -1; } var n = +fromIndex || 0; if ((n) === Infinity) { n = 0; } if (n >= len) { return -1; } k = (n >= 0 ? n : len - (n), 0); while (k < len) { if (k in O && O[k] === searchElement) { return k; } k++; } return -1; }; }
The introduction to the relevant introduction of js that does not support indexof is over here. The above solution is very useful. Friends who need it can refer to the above tutorial. Thank you very much for your support for my website!