此代码适用于 IE10+ 和 Chrome,但我需要它适用于 IE8。
我认为它一直有效,直到我用注释标记的点为止,因为将生成一个下拉菜单并将其缩放到正确的大小,但它不会填充任何可供选择的选项。此外,IE8 调试器不会给出任何错误,它只是无法正确执行。
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);
}
};
在 IE10 上我看到这个:
在 IE8 上我看到这个:
您能在第二个代码块中看到任何可能与 IE8 不兼容并导致问题的内容吗?我一直在注释掉一些内容,但没有结果,因为它们与第一个 block 中使用的命令都是相同的。
预先感谢您的帮助。
最佳答案
我找到了答案。 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);
}
};
关于用于创建 html 选择菜单的 javascript 代码未在 IE8 中填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24542593/