简体   繁体   English

Array.prototype.join在IE 8中不起作用

[英]Array.prototype.join not working in IE 8

I have used the code snippet like below in my working scenario. 我在工作场景中使用了如下代码段。 It's working fine in IE's latest versions but join function is not working in IE 8 Version. 在IE的最新版本中运行正常,但在IE 8版本中,join功能不起作用。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <script type="text/javascript">
            var str = "TEST\n";
            var lines = str.split(/\r?\n/).join("\r\n");
            debugger;        
        </script>
   </body>
</html>

How to resolve this problem? 如何解决这个问题? Thanks in Advance. 提前致谢。

The problem in your particular example would rather be the split() function, not the join() . 在您的特定示例中,问题可能是split()函数,而不是join() The split() is what behaves differently between JScript 6 (IE8) and later JScript versions in that the length of the resulting array is different if the separator actually ends or begins the string. split()是JScript 6(IE8)和更高版本的JScript之间的不同之处,如果分隔符实际上是结束还是开始于字符串,则结果数组的长度是不同的。 In this case, the <null> item is dropped, meaning: 在这种情况下, <null>项将被删除,这意味着:

JScript 6: JScript 6:
"TEST\\n".split("\\n").length equals 1 “ TEST \\ n” .split(“ \\ n”)。length等于1

JScript 6+: JScript 6+:
"TEST\\n".split("\\n").length equals 2 “ TEST \\ n” .split(“ \\ n”)。length等于2

Again, in your example, this means that the join() in IE8 is performed on an array containing just one element. 同样,在您的示例中,这意味着IE8中的join()在仅包含一个元素的数组上执行。

If instead you use replace() --which seems a better option anyway-- you can overcome this problem, ie str.replace(/\\r?\\n/g, "\\r\\n") . 如果改为使用replace()无论如何这似乎是一个更好的选择-您可以克服此问题,即str.replace(/\\r?\\n/g, "\\r\\n")

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

相关问题 如何在reactjs中使用array.prototype.join - How to using array.prototype.join in reactjs 在Javascript中,Array.join真的是Array.prototype.join吗? - In Javascript, is Array.join really Array.prototype.join? 为什么 Array.prototype.join() 会展平任意深度的数组? - Why does Array.prototype.join() flatten an array of any depth? 类似于Java中的Array.prototype.join的地图或平面图 - Map or flatmap that works like Array.prototype.join in Javascript 我怎样才能真正理解&#39;Array.prototype.join&#39;的方法 - How can I really understand the method of 'Array.prototype.join' Array.prototype.join() 在反应 useState 中不起作用 - Array.prototype.join() doesn't work in react useState 除了Array.prototype.join()以外,还有没有更短的方法将数组连接到一个字符串? - Is there a shorter way to join an array to one string besides Array.prototype.join()? 我尝试编写自己的Array.prototype.join()有什么问题? - What's wrong with my attempt to write my own Array.prototype.join()? 为什么 Array.prototype.map 会忽略稀疏数组中的空槽,而 Array.prototype.join 不会? - Why does Array.prototype.map ignore empty slots in a sparse array whereas Array.prototype.join does not? Array.prototype.forEach在IE8中不起作用 - Array.prototype.forEach not working in IE8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM