[英]javascript split not working in IE9 - lower
I'm using IE 11 to emulate older versions of IE. 我正在使用IE 11来模拟IE的旧版本。 The following code does not work as expected in IE 9 and below :
以下代码在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
is logged to the console as undefined
. recBox_val_arr
作为undefined
记录到控制台。
The above code returns an Array
on IE 10 and 11, Firefox, Chrome, Opera and Safari. 上面的代码在IE 10和11,Firefox,Chrome,Opera和Safari上返回一个
Array
。 Why is it not working in IE 9 and below? 为什么在IE 9及更低版本中它不起作用?
Given this situation: 在这种情况下:
$("#search_input").val() === "ab abc";
search_input_val === "ab abc";
recBox_val_arr
is logged as undefined
by IE ≤ 9, [object Array]
by Firefox and ["ab", "abc"]
by other browsers. recBox_val_arr
记录recBox_val_arr
为undefined
,Firefox记录为[object Array]
["ab", "abc"]
,其他浏览器记录为["ab", "abc"]
。
I'm linking to jQuery 1.10.2 via Google's CDN: 我通过Google的CDN链接到jQuery 1.10.2:
//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
I also tried recBox_val_arr = search_input_val.split(' ')
, but recBox_val_arr
is still logged as undefined
. 我也尝试了
recBox_val_arr = search_input_val.split(' ')
,但是recBox_val_arr
仍然记录为undefined
。
console.log
is busted console.log
的实现被破坏 There are indeed bugs with older IE's implementation of split
, but that's not the problem here. 较旧的IE的
split
实现确实存在一些错误,但这不是这里的问题。 In fact, split
is working just fine—the real issue is IE 11's busted implementation of console.log
when emulating IE 9: 实际上,
split
运行得很好-真正的问题是IE 11在模拟IE 9时破坏了console.log
的实现:
console.log("test test".split(/\s+/)) // logs "undefined"
"test test".split(/\s+/) // logs "[object Array]"
More generally, console.log
in IE 11 emulating IE 9 doesn't support logging of objects or arrays: 更一般而言,IE 11中模拟IE 9的
console.log
不支持对象或数组的记录:
console.log("foo"); // logs "foo"
console.log({ foo: "bar" }) // logs "undefined"
console.log(["foo"]) // logs "undefined"
Worst of all, this isn't even comparable with how IE 9 actually behaves. 最糟糕的是,这甚至无法与IE 9的实际行为相提并论。 Here's what you get if you run IE 9 directly on a VM:
如果直接在VM上运行IE 9,将获得以下信息:
console.log("test test".split(/\s+/)) // logs "test,test"
console.log
is totally borked when called on IE 11 emulating IE 9. console.log
完全令人厌烦。 $(document).ready(function(){ $(文件)。就绪(函数(){
$("button").click(function(){ $( “按钮”)。点击(函数(){
var search_input_val = $.trim($("#search_input").val()).replace(/\\s{2,}/g, ' '); var search_input_val = $ .trim($(“#search_input”)。val())。replace(/ \\ s {2,} / g,'');
console.log(search_input_val); 的console.log(search_input_val);
var recBox_val_arr = search_input_val.split(/\\s+/); var recBox_val_arr = search_input_val.split(/ \\ s + /); //--Here--
// - 这里 -
console.log(recBox_val_arr); 的console.log(recBox_val_arr);
console.log(recBox_val_arr[0]); 的console.log(recBox_val_arr [0]);
console.log(recBox_val_arr[1]); 的console.log(recBox_val_arr [1]);
console.log(recBox_val_arr[2]); 的console.log(recBox_val_arr [2]);
}); });
}); });
Create body with 用创建身体
< body> <身体>
< input type="text" id="search_input" /> <input type =“ text” id =“ search_input” />
< button>Submit < /button> <按钮>提交</按钮>
< /body> </ body>
console.log(recBox_val_arr);
的console.log(recBox_val_arr);
Input => hello 123 hi 输入=>你好123嗨
Output => ["hello", "123", "hi"] 输出=> [“ hello”,“ 123”,“ hi”]
Split function in PHP is used to break string into an array.
PHP中的split函数用于将字符串分成数组。
console.log(recBox_val_arr[0]); 的console.log(recBox_val_arr [0]); == hello
== 你好
console.log(recBox_val_arr[1]); 的console.log(recBox_val_arr [1]); == 123
== 123
console.log(recBox_val_arr[2]); 的console.log(recBox_val_arr [2]); == hi
== 嗨
If you put console.log() outside $("button").click(function(){});
如果将console.log()放在$(“ button”)。click(function(){})之外;
then the value will be initially shown as "undefined".
那么该值将最初显示为“未定义”。
Because it was not able to get value from text box. 因为它无法从文本框中获取价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.