奇怪的行为,我只是发布这个问题,看看是否有人知道这个的原因,或者我的代码是否完全错误:
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“错误”吗?
最佳答案
访问类似字符串数组的方式在 ECMAScript 3 中不是标准的:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String#section_5
Array-like character access (the second way above) is not part of ECMAScript 3. It is a JavaScript and ECMAScript 5 feature.
你所做的是拆分字符串:
var textChars = text.split('');
alert(textChars[0]);
关于javascript - IE7 Javascript 和使用字符串作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397283/