The indexOf () method returns the position of the first occurrence of a specified string value. This article introduces how ie8 does not support indexof.
IndexOf () method definition and usage
The indexOf () method returns the position of the first occurrence of a specified string value.
This method retrieves the stringObject string from start to end to see if it contains the searchvalue. The start position is at the string's fromindex or the start of the string (when fromindex is not specified ). If a searchvalue is found, the first position of the searchvalue is returned.
The character position in stringObject starts from 0.
If the string is not found in the array,-1 is returned.
Enter the subject:
The indexof method in js looks for the first index value that can be found in the array for a given element, but indexof is not supported in IE8. This article introduces the solution that ie8 does not support indexof.
How does a browser not support indexof? When writing a scripts, you can use the following code at the beginning, which allows you to use the indexOf method without local support.
if (!Array.prototype.indexOf) {Array.prototype.indexOf = function(searchElement, fromIndex) {var k;if (this == null) {throw new TypeError('"this" is null or not defined');}var O = Object(this);var len = O.length >>> 0;if (len === 0) {return -1;}var n = +fromIndex || 0;if (Math.abs(n) === Infinity) {n = 0;}if (n >= len) {return -1;}k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);while (k < len) {if (k in O && O[k] === searchElement) {return k;}k++;}return -1;};}
Js does not support indexof. This is the end of the introduction. The above solution is very useful. If you need it, please refer to the above tutorial. Thank you very much for your support for the script home website!