3 回答
TA貢獻1815條經驗 獲得超10個贊
使用克隆節點:
document.body.appendChild(document.querySelector('#select').cloneNode(true));它克隆該元素,然后將克隆附加到頁面主體。您不需要獲取元素的值。
希望這個對你有幫助。
TA貢獻2041條經驗 獲得超4個贊
我認為最好給出每個選項的價值,
也許這個js可以幫助你
與 HTML
<!DOCTYPE html><html>
<select id="select">
<option value="Bulldog">Bulldog</option>
<option value="Pitbull">Pitbull</option>
</select>
<!--The entire dropdown with all its child option elements should be duplicated into the div element:-->
<div id="div"></div>
</html>
和 JS :
var j = document.getElementById("select");
var i;
var value = "List Value: ";
for (i = 0; i < j.length; i++) {
value = value + "\n" + j.options[i].text;
}
console.log(value)
TA貢獻1801條經驗 獲得超16個贊
下面的代碼通過克隆整個元素并附加到元素 id="div" 來顯示結果。讓我知道這是否是您所需要的。
<!DOCTYPE html>
<html>
<body>
<select id="select">
<option>Bulldog</option>
<option>Pitbull</option>
</select>
<!--The entire dropdown with all its child option elements should be duplicated into the div element:-->
<div id="div"></div>
</body>
</html>
<script>
var x = document.querySelector("#select").cloneNode(true);
document.getElementById("div").appendChild(x);
</script>
添加回答
舉報
