我使用 IE 11 来模拟旧版本的 IE。以下代码在 IE 9 及更低版本中无法按预期工作:
var search_input_val = $.trim($("#search_input").val()).replace(/\s{2,}/g, ' ');
console.log(search_input_val);
var recBox_val_arr = search_input_val.split(/\s+/); // HERE
console.log(recBox_val_arr);
recBox_val_arr
在控制台中记录为 undefined
。
以上代码在 IE 10 和 11、Firefox、Chrome、Opera 和 Safari 上返回一个 Array
。为什么它不能在 IE 9 及更低版本中运行?
更多详细信息
鉴于这种情况:
$("#search_input").val() === "ab abc";
search_input_val === "ab abc";
recBox_val_arr
在 IE ≤ 9 中记录为 未定义
,在 Firefox 中记录为 [object Array]
,["ab", "abc"]
通过其他浏览器。
我通过 Google 的 CDN 链接到 jQuery 1.10.2:
//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
我还尝试了 recBox_val_arr = search_input_val.split(' ')
,但 recBox_val_arr
仍然记录为 undefined
。
最佳答案
IE 11 对 IE 9 console.log
的实现已失效
旧版 IE 的 split
实现确实存在错误,但这不是这里的问题。事实上,split
工作得很好,真正的问题是 IE 11 在模拟 IE 9 时破坏了 console.log
的实现:
console.log("test test".split(/\s+/)) // logs "undefined"
"test test".split(/\s+/) // logs "[object Array]"
就像真的,真的失败了
更一般地说,IE 11 中模拟 IE 9 的 console.log
不支持对象或数组的日志记录:
console.log("foo"); // logs "foo"
console.log({ foo: "bar" }) // logs "undefined"
console.log(["foo"]) // logs "undefined"
即使是 IE 9 也没有这么糟糕
最糟糕的是,这甚至无法与 IE 9 的实际行为相比较。如果直接在虚拟机上运行 IE 9,您将得到以下结果:
console.log("test test".split(/\s+/)) // logs "test,test"
摘要
- IE 11 对 IE 9 的模拟并不完美。
console.log
在模拟 IE 9 的 IE 11 上调用时完全崩溃。- 始终使用虚拟机 ( freely available to download ) 进行可靠的跨浏览器测试。
关于javascript split 在 IE9 中不起作用 - 较低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395060/