我正在使用innerHTML練習打印到html頁面。我試圖從選擇標記中使用項目符號列表打印ID爲printHere
的段落中的值。到目前爲止,我可以獲得價值,但我不知道該從哪裏去。如何從選擇標記中的值打印項目符號列表
我的HTML
<!DOCTYPE html>
<html>
<head><title></title><meta charset="UTF-8"></head>
<body>
<h3> Pick your fruit to print it to the menu below</h3>
<select id="fruitSelect">
<option value = "banana" >banana</option>
<option value = "strawberry">strawberry</option>
<option value = "apple">apple</option>
</select>
<p id="printHere"></p>
<button type="button" onclick = "print()">Select</button>
</body>
</html>
而且我的腳本
<script>
function print(){
var x = document.getElementById("fruitSelect");
console.log(x.value);//test to see if it works
document.getElementById("printHere").innerHTML=x;
}
</script>
的「setAsList」功能和「addToList()」之間的差別是僅在「.innerHTML」,即的設定中使用的操作者,在使用一方面‘=’設置innerHTML,另一方面,'+ ='用於添加到現有的innerHTML中。 – dylansweb