javascript拆分在IE9中不起作用 - 更低 [英] javascript split not working in IE9 - lower
问题描述
我正在使用IE 11来模拟旧版本的IE。以下代码在 IE 9及以下中无法正常工作:
I'm using IE 11 to emulate older versions of IE. The following code does not work as expected in IE 9 and below:
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。为什么它不能在IE 9及以下版本中运行?
The above code returns an Array
on IE 10 and 11, Firefox, Chrome, Opera and Safari. Why is it not working in IE 9 and below?
鉴于这种情况:
$("#search_input").val() === "ab abc";
search_input_val === "ab abc";
recBox_val_arr
记录为未定义
按IE≤9, [对象数组]
由Firefox和 [ab,abc]其他浏览器
。
recBox_val_arr
is logged as undefined
by IE ≤ 9, [object Array]
by Firefox and ["ab", "abc"]
by other browsers.
我通过谷歌的CDN链接到jQuery 1.10.2:
I'm linking to jQuery 1.10.2 via Google's CDN:
//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
我也试过 recBox_val_arr = search_input_val.split('')
,但 recBox_val_arr
仍然记录为 undefined
。
I also tried recBox_val_arr = search_input_val.split(' ')
, but recBox_val_arr
is still logged as undefined
.
推荐答案
IE 11的IE 9的 console.log
的实现被破坏
确实存在错误旧的IE实现 split
,但这不是问题所在。事实上, split
工作得很好 - 真正的问题是IE 11在模拟IE时错误地执行 console.log
9:
IE 11's implementation of IE 9's console.log
is busted
There are indeed bugs with older IE's implementation of split
, but that's not the problem here. In fact, split
is working just fine—the real issue is IE 11's busted implementation of console.log
when emulating IE 9:
console.log("test test".split(/\s+/)) // logs "undefined"
"test test".split(/\s+/) // logs "[object Array]"
更一般地说,<$模拟IE 9的IE 11中的c $ c> console.log 不支持记录对象或数组:
More generally, console.log
in IE 11 emulating IE 9 doesn't support logging of objects or arrays:
console.log("foo"); // logs "foo"
console.log({ foo: "bar" }) // logs "undefined"
console.log(["foo"]) // logs "undefined"
最糟糕的是,这甚至与IE 9 实际上的行为无法相提并论。如果你直接在VM上运行IE 9,这就是你得到的:
Worst of all, this isn't even comparable with how IE 9 actually behaves. Here's what you get if you run IE 9 directly on a VM:
console.log("test test".split(/\s+/)) // logs "test,test"
- IE 11对IE 9的仿真并不完美。
-
console.log
在IE 11上模拟IE 9时被调用。 - 始终使用VM(可免费下载)以进行可靠的跨浏览器测试。
- IE 11's emulation of IE 9 isn't perfect.
console.log
is totally borked when called on IE 11 emulating IE 9.- Always use a VM (freely available to download) for reliable cross-browser testing.
这篇关于javascript拆分在IE9中不起作用 - 更低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!