动态设置“在 IE 上选择”的选项

Dynamically set the OPTIONS for Select on IE

本文关键字:选择 选项 IE 设置 动态      更新时间:2023-09-26

>我使用从 Ajax 调用获得的数据设置了几乎 10+ 选择控件;以下是我的代码:

function arrayToOptionList(list) {
    var optList = [];
    for (i = 0; i < list.length; i++) {
        optList.push("<option value ='" + list[i][1] + "' >" + list[i][0] + "</option>");
    }
    return optList.join("");
}
 opt = arrayToOptionList(list);
 $("select_ctrl_id").update(opt);

我知道IE在dom操作方面存在重大问题。经过几次谷歌搜索,我已经针对IE优化了我的代码。设置选择控件时,我的IE仍然有一段时间没有响应。你们能建议我还能做些什么来改善这个问题吗?

我正在使用原型.js来设置选择控件,并且在IE 8/9上遇到了问题

根据我的经验,使用 innerHTML 最适合 IE,因此请尝试将其与普通的 DOM 脚本结合使用:

document.querySelector('#select_ctrl_id').innerHTML = opt.join('');

JSFiddle

尝试使用原型提供的内置方法 - 例如

var select = $("select_ctrl_id").clone();
list.each(function(i){
    select.insert(new Element('option',{'value':i[1]}).update(i[0]));
});
$("select_ctrl_id").replace(select);

这将创建 select DOM 元素的副本,并在选择中插入所有选项,然后替换选择。

尝试旧时尚的方式 http://www.w3schools.com/jsref/met_select_add.asp

var x=document.getElementById("mySelect");
var option=document.createElement("option");
option.text="Kiwi";
try
  {
  // for IE earlier than version 8
  x.add(option,x.options[null]);
  }
catch (e)
  {
  x.add(option,null);
  }