简体   繁体   English

javascript拆分在IE9中不起作用-较低

[英]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及更低版本中它不起作用?

More details 更多细节

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_arrundefined ,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

IE 11's implementation of IE 9's console.log is busted IE 11的IE 9 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]"

带有和不带有console.log调用的IE 11(模拟IE 9)控制台输出的屏幕截图,用于“测试测试” .split

Like really, really busted 就像真的,真的破灭一样

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"

IE 11(模拟IE 9)console.log输出的字符串,数组和对象的屏幕快照

Even IE 9 wasn't this bad 甚至IE 9也不错

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"

IE 9 console.log输出的“测试测试” .split屏幕快照(/ \\ s + /)

Summary 摘要

  • IE 11's emulation of IE 9 isn't perfect. IE 11对IE 9的模仿并不完美。
  • console.log is totally borked when called on IE 11 emulating IE 9. 当在IE 11上模拟IE 9时, console.log完全令人厌烦。
  • Always use a VM ( freely available to download ) for reliable cross-browser testing. 始终使用VM( 可免费下载 )进行可靠的跨浏览器测试。

$(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM