简体   繁体   English

使用javascript在IE5中使用数组推送不起作用?

[英]Array push not working in IE5 using javascript?

When I try to load the select options into the optStored array using the array push, the array always remains empty. 当我尝试使用数组push将select选项加载到optStored数组时,该数组始终保持为空。 In chrome, firefox, and safari the code is working correctly but not in Internet Explorer. 在chrome,firefox和safari中,代码工作正常但不在Internet Explorer中。 Is there a work around for this or nothing can be done about this? 有没有解决这个问题或者没有办法解决这个问题?

Updated 2:12am 10/11/16: Made some changes to the code, previous code is commented out but the array still remains empty! 上午2:12更新于10/11/16:对代码进行了一些更改,之前的代码被注释掉了,但数组仍然是空的! Is there a solution? 有解决方案吗?

 // array to store select options var optStored = []; // filter select function filterFruits(el) { var value = el.value.toLowerCase(); var form = el.form; var opt, sel = form.fruits; if (value == "") { restoreOpts(); } else { for (var i = sel.options.length - 1; i >= 0; i--) { opt = sel.options[i]; if (opt.text.toLowerCase().indexOf(value) == -1) { sel.removeChild(opt); } } } } // Restore select options function restoreOpts() { var sel = document.getElementById('fruits'); sel.options.length = 0; for (var i = 0, iLen = optStored.length; i < iLen; i++) { // Recent Update 2:12am 10/11/16: // Found aworkaround for restoring the select options // This method works with versions of IE4+ sel.options[sel.options.length] = new Option(optStored[i].text, optStored[i].value, false, false); // Recent Comment Update 2:12am 10/11/16: // Console complains of a type mismatch error // sel.add(optStored[i], null); } } // Load select options window.onload = function() { var sel = document.getElementById('fruits'); for (var i = 0, iLen = sel.options.length; i < iLen; i++) { // Recent Comment Update 2:12am 10/11/16: // tried this method as well but no luck. // it was also mentioned in the comments below // the array still remains empty! optStored[i] = sel.options[i]; //optStored.push(sel.options[i]); } }; 
 <form> <input type="text" onkeyup="filterFruits(this);" placeholder="Filter"> <select id="fruits"> <option value="1">Apple</option> <option value="2">Orange</option> <option value="3">Cherry</option> <option value="4">Banana</option> </select> </form> 

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Browser_compatibility , IE does support the push method only as of version 5.5. 根据https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Browser_compatibility,IE仅支持版本5.5的push方法。 Try the following instead: 请尝试以下方法:

window.onload = function() {
  var sel = document.getElementById('fruits');
  for (var i = 0, iLen = sel.options.length; i < iLen; i++) {
    optStored[i] = sel.options[i];
  }
};

If you press F12, does it give any errors in the console? 如果按F12,它是否在控制台中出现任何错误? The IE console is nowhere near as good as the Chrome/Firefox developer tools, but it should display any errors. IE控制台远不及Chrome / Firefox开发人员工具,但它应该显示任何错误。

I'm wondering about passing undefined as the second argument to the add method, according to the docs it should null to append the new option to the end of the list 我想知道将undefined作为第二个参数传递给add方法,根据文档它应该为null将新选项附加到列表的末尾

before is optional and an element of the collection, or an index of type long, representing the item item should be inserted before. before是可选的,并且应该在之前插入表示项目项的集合的元素或类型为long的索引。 If this parameter is null (or the index does not exist), the new element is appended to the end of the collection. 如果此参数为null(或索引不存在),则新元素将附加到集合的末尾。

https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/add https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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