简体   繁体   English

Javascript Array在IE7和更早版本中不起作用,但在IE10中起作用

[英]Javascript Array not working in IE7 and older versions but working in IE10

Javascript Array not working in IE7 and older versions but working in IE10 Here is my code which is working in IE10 but not working in IE7. Javascript Array在IE7和更早版本中不起作用,但在IE10中起作用这是我的代码,该代码在IE10中起作用,但在IE7中不起作用。

    number = "123";
    var j=0;
    alert(number[j]);

Result in IE7 IE7中的结果

undefined 未定义

Result in IE10 IE10中的结果

1 1

Please let me know how to call the variable values as single dimension array in older browsers. 请让我知道如何在较旧的浏览器中将变量值称为单维数组。

Javascript Array not working in IE7 Javascript数组在IE7中不起作用

This is not an array “not working” – you have a string value here (the browser will convert it to one, because for numbers no such kind of access is defined), and are trying to access it's single characters with a zero-based index. 这不是“无效”的数组 –您在此处有一个字符串值(浏览器会将其转换为1,因为对于数字,未定义此类访问权限),并尝试使用从零开始的单个字符指数。

Older IE simply don't support this – so cast it to a string (implicitly/explicitly) and then use the charAt method of the String object instead . 较旧的IE根本不支持此功能-因此将其charAt转换为字符串(隐式/显式),然后使用String对象的charAt方法。

(''+number)[j]应该可以在任何浏览器中使用,前提是该number仍然可以强制转换为String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM