为什么`mystring [0]`在IE中没有返回任何内容?

Kal*_*exx 3 javascript internet-explorer

所以在我的javascript中我有以下代码:

    var wholeHash = window.location.hash.substring(1);
    var data = new Object();

    // Remove the bang or slash if one appears at the beginning
    if (wholeHash[0] == '!') { wholeHash = wholeHash.substring(1); }
    if (wholeHash[0] == '/') { wholeHash = wholeHash.substring(1); }
Run Code Online (Sandbox Code Playgroud)

当这个即将运行时,wholeHash其值为"/search/&&stype=quick".但是,wholeHash[0]什么都不返回,这会导致wholeHash[0] == '!'错误.这只是IE中的情况.

为什么是这样?我知道我可以使用startswith但我通常感兴趣为什么IE不能获得字符串的单个字符,而其他浏览器可以.

Mat*_*eer 6

因为索引到具有数组样式索引的字符串是新的,而旧版本的IE缺少这一点.相反,mystring.charAt(0)如果您需要在8之前支持IE,则需要使用.

  • @KallDrexx使用`<meta http-equiv ="x-ua-compatible"content ="ie = edge"/>`,你会得到它,还有更好的东西. (2认同)