JavaScript 拆分功能在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6422355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:40:22  来源:igfitidea点击:

JavaScript split function not working in IE

javascriptinternet-explorer

提问by V_B

I am using the split function in JavaScript. It works fine in Firefox and Chrome, but IE displays an error when I call the split function. Is there a way to use other function like split?

我在 JavaScript 中使用 split 函数。它在 Firefox 和 Chrome 中运行良好,但是当我调用 split 函数时 IE 显示错误。有没有办法使用像split这样的其他功能?

回答by Raynos

split Method

拆分方法

It's fully supported by IE8

IE8 完全支持

split method for JScript 5.6

JScript 5.6 的 split 方法

It's also fully supported by IE6

IE6 也完全支持

Live exampleusing .split(/\s+/)

使用现场示例.split(/\s+/)

Tested in IE9 standards, IE9 IE8 mode, IE9 IE7 mode and IE9 quirks mode. All work.

在 IE9 标准、IE9 IE8 模式、IE9 IE7 模式和 IE9 怪癖模式下测试。所有的工作。

Edit:

编辑:

Turns out your actual problem is using .textContent. This does not work in IE. There are two alternatives.

原来你的实际问题是使用.textContent. 这在 IE 中不起作用。有两种选择。

Feature detection:

特征检测:

var str;
if (el.textContent) {
  str = el.textContent;
} else {
  str = el.innerText;
}

.nodeValue:

.nodeValue:

var str = el.nodeValue;

var str = el.nodeValue;

回答by Kris

When you split a number instead of String, Javascript throws - Object doesn't support this property.

当您拆分数字而不是字符串时,Javascript 会抛出 - Object 不支持此属性。

Make sure you have a string value in var.

确保在 var 中有一个字符串值。

回答by kennebec

There is at least one difference between IE below #9 and most other browsers when dealing with string splitting-

在处理字符串拆分时,#9 以下的 IE 与大多数其他浏览器之间至少有一个区别 -

var s='In some browsers, you can "split" a string on a parenthized delimeter and return the "split-off" bits in the array.';

var s='在某些浏览器中,您可以在带括号的分隔符上“拆分”字符串并返回数组中的“拆分”位。';

s.split(/( ?['"] ?)/).join('\n')

/***************************************************/
Firefox 4.0.1>>
 In some browsers, you can
 "
split
" 
a  string on a parenthized delimeter and return the
 "
split-off
" 
bits in the array.
/***************************************************/    
MSIE 8.0>>
 In some browsers, you can
split
a  string on a parenthized delimeter and return the
split-off
bits in the array.
/***************************************************/    
MSIE 9.0>>
 In some browsers, you can
 "
split
" 
a  string on a parenthized delimeter and return the
 "
split-off
" 
bits in the array.