用于创建未在IE8中填充的html选择菜单的javascript代码
这段代码适用于IE10+和Chrome,但我需要它来适用于IE8 我认为它会一直工作到我用注释标记的点,因为会生成一个下拉菜单,并将其缩放到正确的大小,但它不会填充任何可选择的选项。此外,IE8调试器没有给出任何错误,只是执行不正确用于创建未在IE8中填充的html选择菜单的javascript代码,javascript,html,internet-explorer,select,internet-explorer-8,Javascript,Html,Internet Explorer,Select,Internet Explorer 8,这段代码适用于IE10+和Chrome,但我需要它来适用于IE8 我认为它会一直工作到我用注释标记的点,因为会生成一个下拉菜单,并将其缩放到正确的大小,但它不会填充任何可选择的选项。此外,IE8调试器没有给出任何错误,只是执行不正确 create_menu("letter", "letters_select_menu", ["a","b","c"]); function create_menu(div_id, sel_id, menu_options) { var myDiv = d
create_menu("letter", "letters_select_menu", ["a","b","c"]);
function create_menu(div_id, sel_id, menu_options) {
var myDiv = document.getElementById(div_id);
// create select menu and size
var selectList = document.createElement("select");
selectList.setAttribute("id", sel_id);
selectList.setAttribute("size", menu_options.length);
myDiv.appendChild(selectList);
// i think it works up until this point
// populate the menu with the options from the array
for (var i = 0; i < menu_options.length; i++) {
var option = document.createElement("option");
option.setAttribute("value", menu_options[i]);
option.text = menu_options[i];
selectList.appendChild(option);
}
};
创建菜单(“字母”、“字母”选择菜单“,[“a”、“b”、“c”);
函数创建菜单(div\u id、sel\u id、菜单选项){
var myDiv=document.getElementById(div_id);
//创建选择菜单和大小
var selectList=document.createElement(“选择”);
selectList.setAttribute(“id”,sel_id);
选择List.setAttribute(“大小”,菜单选项。长度);
myDiv.appendChild(选择列表);
//我认为在这一点之前都是有效的
//使用阵列中的选项填充菜单
对于(变量i=0;i
在IE10上,我看到:
在IE8上,我看到:
你能在第二段代码中看到任何可能与IE8不兼容并导致问题的东西吗?我一直在注释内容,但没有结果,因为它们都是第一个块中使用的相同命令
提前感谢您的帮助。我找到了答案
option.text
在IE8或IE9中不起作用,所以我将其更改为option.innerHTML
,现在它可以工作了
关于最终结果:
function create_menu(div_id, sel_id, menu_options) {
var myDiv = document.getElementById(div_id);
// create select menu and size
var selectList = document.createElement("select");
selectList.setAttribute("id", sel_id);
selectList.setAttribute("size", menu_options.length);
myDiv.appendChild(selectList);
// populate the menu with the options from the array
for (var i = 0; i < menu_options.length; i++) {
var option = document.createElement("option");
option.setAttribute("value", menu_options[i]);
option.innerHTML = menu_options[i];
selectList.appendChild(option);
}
};
函数创建菜单(div\u id、sel\u id、菜单选项){
var myDiv=document.getElementById(div_id);
//创建选择菜单和大小
var selectList=document.createElement(“选择”);
selectList.setAttribute(“id”,sel_id);
选择List.setAttribute(“大小”,菜单选项。长度);
myDiv.appendChild(选择列表);
//使用阵列中的选项填充菜单
对于(变量i=0;i
使用select的“添加”功能
var sel = document.createElement('select');
var option = documeng.createElement('option');
option.value = '1';
option.text = 'Option 1';
sel.add(option, null);
innerHTML可能不安全(xss)。True。出于这个目的,这应该没问题,但是您推荐一种替代方法吗?使用jQuery:$(option).text(menu_options[i]);