本文翻译自:Why doesn't indexOf work on an array IE8?

The below function works fine on Opera, Firefox and Chrome. 以下功能在Opera,Firefox和Chrome上正常运行。 However, in IE8 it fails on the if ( allowed.indexOf(ext[1]) == -1) part. 但是,在IE8中,它在if ( allowed.indexOf(ext[1]) == -1)部分失败。

Does anyone know why? 有人知道为什么吗? Is there any obvious mistake? 有没有明显的错误?

function CheckMe() {var allowed = new Array('docx','xls','xlsx', 'mp3', 'mp4', '3gp', 'sis', 'sisx', 'mp3', 'wav', 'mid', 'amr', 'jpg', 'gif', 'png', 'jpeg', 'txt', 'pdf', 'doc', 'rtf', 'thm', 'rar', 'zip', 'htm', 'html', 'css', 'swf', 'jar', 'nth', 'aac', 'cab', 'wgz');var fileinput=document.getElementById('f');var ext = fileinput.value.toLowerCase().split('.');if ( allowed.indexOf(ext[1]) == -1) {document.getElementById('uploadsec').innerHTML = document.getElementById('uploadsec').innerHTML;alert('This file type is not allowed!');}
}

#1楼

参考:https://stackoom.com/question/FE7D/为什么indexOf在数组IE-上不起作用


#2楼

Please careful with $.inArray if you want to use it. 如果要使用$ .inArray,请小心。 I just found out that the $.inArray is only works with "Array", not with String. 我刚刚发现$ .inArray仅适用于“ Array”,不适用于String。 That's why this function will not working in IE8! 这就是为什么此功能在IE8中不起作用的原因!

The jQuery API make confusion jQuery API令人困惑

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. $ .inArray()方法类似于JavaScript的本机.indexOf()方法,因为它找不到匹配项时返回-1。 If the first element within the array matches value, $.inArray() returns 0 如果数组中的第一个元素匹配值,则$ .inArray()返回0

--> They shouldn't say it "Similar". ->他们不应该说“类似”。 Since indexOf support "String" also! 由于indexOf支持“字符串”也!


#3楼

If you're using jQuery and want to keep using indexOf without worrying about compatibility issues, you can do this : 如果您使用的是jQuery并希望继续使用indexOf而不用担心兼容性问题,则可以执行以下操作:

if (!Array.prototype.indexOf) {Array.prototype.indexOf = function(val) {return jQuery.inArray(val, this);};
}

This is helpful when you want to keep using indexOf but provide a fallback when it's not available. 当您想继续使用indexOf但在不可用时提供后备功能时,这很有用。


#4楼

You can use this to replace the function if it doesn't exist: 您可以使用它来替换该函数(如果不存在):

<script>
if (!Array.prototype.indexOf) {Array.prototype.indexOf = function(elt /*, from*/) {var len = this.length >>> 0;var from = Number(arguments[1]) || 0;from = (from < 0) ? Math.ceil(from) : Math.floor(from);if (from < 0)from += len;for (; from < len; from++) {if (from in this && this[from] === elt)return from;}return -1;};
}
</script>

#5楼

The problem 问题

IE<=8 simply doesn't have an indexOf() method for arrays. IE <= 8根本没有用于数组的indexOf()方法。


The solution 解决方案

If you need indexOf in IE<=8, you should consider using the following polyfill , which is recommended at the MDN : 如果您需要IE <= 8中的indexOf ,则应考虑使用以下polyfill ,这是MDN推荐的 :

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;};
}

Minified : 缩小:

Array.prototype.indexOf||(Array.prototype.indexOf=function(r,t){var n;if(null==this)throw new TypeError('"this" is null or not defined');var e=Object(this),i=e.length>>>0;if(0===i)return-1;var a=+t||0;if(Math.abs(a)===1/0&&(a=0),a>=i)return-1;for(n=Math.max(a>=0?a:i-Math.abs(a),0);i>n;){if(n in e&&e[n]===r)return n;n++}return-1});

#6楼

Versions of IE before IE9 don't have an .indexOf() function for Array, to define the exact spec version , run this before trying to use it: IE9之前的IE版本没有用于Array的.indexOf()函数,以定义确切的规范版本 ,请在尝试使用它之前运行它:

if (!Array.prototype.indexOf)
{Array.prototype.indexOf = function(elt /*, from*/){var len = this.length >>> 0;var from = Number(arguments[1]) || 0;from = (from < 0)? Math.ceil(from): Math.floor(from);if (from < 0)from += len;for (; from < len; from++){if (from in this &&this[from] === elt)return from;}return -1;};
}

This is the version from MDN , used in Firefox/SpiderMonkey. 这是MDN的版本,在Firefox / SpiderMonkey中使用。 In other cases such as IE, it'll add .indexOf() in the case it's missing... basically IE8 or below at this point. 在其他情况下,例如IE,它会在丢失的情况下添加.indexOf() ...此时基本上是IE8或更低版本。

为什么indexOf在数组IE8上不起作用?相关推荐

  1. 解决IE8上传文件时javascript取文件的本地路径的问题C:\fakepath\..

    IE8上传文件时javascript读取文件的本地路径的问题("C:\fakepath\")的解决方案 前一阵把IE6升级到了IE8,没想到一个网站里面的上传图片时用JavaScr ...

  2. boost::sort模块实现在大多数排序的数组示例上展开排序

    boost::sort模块实现在大多数排序的数组示例上展开排序 实现功能 C++实现代码 实现功能 boost::sort模块实现在大多数排序的数组示例上展开排序 C++实现代码 #include & ...

  3. feachall php_集合:给 PHP 数组插上翅膀

    集合:给 PHP 数组插上翅膀 由 学院君 创建于2年前, 最后更新于 9个月前 版本号 #2 35657 views 7 likes 1 collects 简介 Illuminate\Support ...

  4. C语言 输入一个5行5列的数组。1.求数组主对角线上元素的和。2.求出辅对角线上元素的积。3.找出主对角线上最大的值及其位置

    C语言 输入一个5行5列的数组.1.求数组主对角线上元素的和.2.求出辅对角线上元素的积.3.找出主对角线上最大的值及其位置 主对角线是什么: 在一个n阶方阵(或是n阶行列式)中,从左上角到右下角这一 ...

  5. c语言二维数组对角线输出字符,输出二维数组对角线上的数 c语言 二维数组 对角线相加...

    1.编写程序,把下面的数据输入到一个二维数组中: 25 36 78 13 12 26 8我自己编的,用VISUAL C++6.0 测试过了 #include"stdio.h" vo ...

  6. switch语句能否作用在byte上,能否作用在long上,能否作用在String上?

    可以用 char, byte, short, int,Character, Byte, Short, Integer String, enum 不能用 Long,boolean,float,doubl ...

  7. android input 点击事件失效,React Native:TextInput元素上的onContentSizeChange事件在Android上不起作用...

    我在Android设备上的TextInput事件'onContentSizeChange'出现问题. 实际上,当我输入消息直到行尾并且文本进入下一行时,TextInput的高度不会更新.React N ...

  8. 键盘上每个键作用!!! (史上最全的)­

    键盘上每个键作用!!! (史上最全的)­ F1帮助 ­ F2改名 ­ F3搜索 ­ F4地址 ­ F5刷新 ­ F6切换 ­ F10菜单 ­ CTRL+A全选 ­ CTRL+C复制 ­ CTRL+X剪 ...

  9. win10 休眠设置无效_睡眠模式在Windows 10系统上不起作用?

    睡眠模式在Windows 10上不起作用?据报道,Windows 10存在许多关于睡眠模式的问题,例如睡眠模式丢失或灰显或睡眠模式失效而没有确切的原因. 由于睡眠模式在Windows 10中非常重要, ...

最新文章

  1. mycat配置访问oracle_教程 | MySql都会了,确定不学习一下MyCat分片?
  2. tensorflow linux多卡训练_用Windows电脑训练深度学习模型?超详细配置教程来了
  3. java 枚举工厂_在Java中使用枚举工厂,最佳做法?
  4. 关于爬虫中常见的两个网页解析工具的分析 —— lxml / xpath 与 bs4 / BeautifulSoup...
  5. Spring Cloud 2020年路线图发布,涵盖Spring Boot 2.3、2.4,Spring Cloud Ilford等重磅内容!
  6. 51. N皇后/52. N皇后 II
  7. v8声卡调音软件_声卡出现杂音怎么办?教你几招解决杂音问题
  8. 1095 解码PAT准考证 (25分)
  9. python对矩阵对角线进行赋值
  10. 【纪念】我的大学同学名单
  11. 文章原创率只有40%?修改方法来了
  12. P值计算(Excel)
  13. 2022年全球光纤机械接头市场前景分析及研究报告
  14. Photoshop调色教程
  15. 无线网络的各种协议标准和区别
  16. 工信部总工程师:建设网络强国振兴实体经济
  17. 计算机图形学3——Boundary-Fill Algorithm
  18. 由例子程序给出的显示列表,展示重定形函数的应用
  19. 线段,射线,直线的关系
  20. STM32超低功耗入门之待机模式

热门文章

  1. Android系统中Parcelable和Serializable的区别
  2. c detail of macro
  3. Binder跨进程通信原理(二):内存映射mmap原理分析
  4. PathComposePathEffectView 使用
  5. Android 10.0 PackageManagerService(一)工作原理及启动流程-[Android取经之路]
  6. 如何取消或定制当点击GridView 的时候出现的那个黄色背景
  7. 内存屏障在硬件层面的实现原理
  8. Flutter开发使用PlatformView显示iOS原生View(50)
  9. (0010) iOS 开发之UI布局兼容 4s/5/6/7 屏幕解决方案
  10. 卡尔曼滤波算法c语言stm32,卡尔曼滤波算法及C语言实现_源代码