如何将生成的选择标记转换为无序列表?

时间:2015-06-17 12:00:27

标签: javascript jquery

我在javascript中使用select标签创建了一些下拉框,现在我想将它转换为无序列表。我怎么能这样做?

http://jsfiddle.net/4pwvg/

这是我的代码

var myDiv = document.getElementById("myDiv");

//Create array of options to be added
var array = ["Volvo","Saab","Mercades","Audi"];

//Create and append select list
var selectList = document.createElement("select");
selectList.setAttribute("id", "mySelect");
myDiv.appendChild(selectList);

//Create and append the options
for (var i = 0; i < array.length; i++) {
    var option = document.createElement("option");
    option.setAttribute("value", array[i]);
    option.text = array[i];
    selectList.appendChild(option);
}

1 个答案:

答案 0 :(得分:0)

尝试

var myDiv = document.getElementById("myDiv");

//Create array of options to be added
var array = ["Volvo","Saab","Mercades","Audi"];

//Create and append select list
var selectList = document.createElement("ol");
selectList.setAttribute("id", "mySelect");
myDiv.appendChild(selectList);

//Create and append the options
for (var i = 0; i < array.length; i++) {
    var option = document.createElement("li");
    option.innerHTML = array[i];
    selectList.appendChild(option);
}

Fiddle