【问题标题】:IE7 Javascript and using string as an arrayIE7 Javascript 并使用字符串作为数组
【发布时间】:2011-12-06 08:52:55
【问题描述】:

奇怪的行为,我只是发布这个问题,看看是否有人知道原因或我的代码是否完全错误:

string text = "~"; //yip, let's take some weird character
alert(text[0]);
//all major browsers output "~"
//IE6 & 7: undefined
alert(text.charAt(0));
//works in all browsers

现在我的问题是:在 IE7 中不支持将文本用作数组吗?代码一般是错误的吗?可以使用 .charAt(i) 代替 string[i] 吗?

PS:有some guy who answered his own question 与此有关。我的问题仍然存在:这是在哪里记录的?这是一个普通的 IE “bug”吗?

【问题讨论】:

    标签: javascript arrays string internet-explorer


    【解决方案1】:

    在 ECMAScript 3 中不标准访问字符串数组:

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#section_5

    类似数组的字符访问(上面的第二种方式)不是 ECMAScript 3。它是 JavaScript 和 ECMAScript 5 的一项功能。

    你所做的是拆分字符串:

    var textChars = text.split('');
    alert(textChars[0]);
    

    【讨论】:

    • 完美!所以 IE 做得对,所有其他浏览器都对我使用字符串作为数组很好。
    猜你喜欢
    • 2017-02-07
    • 2011-05-02
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2019-07-29
    相关资源
    最近更新 更多