我会在html id处显示值列表数组 为什么控制台告诉我show是null并且dosent显示它们?!
<script>
var howMany = prompt("how many do you want to buy?");
var goods = "";
var list = [];
for (var i = 0 ; i < howMany ; i++){
goods = prompt("add a name ?");
list.push(goods);
}
var show = document.getElementById("des");
show.innerText = list;
答案 0 :(得分:1)
可能是由于获取DOM中不存在的“元素”而导致的。 在您的正文标记后调用您的Javascript代码。 e.g
<body>
<div id="des"></div>
</body>
<script>
var howMany = prompt("how many do you want to buy?");
var goods = "";
var list = [];
for (var i = 0 ; i < howMany ; i++){
goods = prompt("add a name ?");
list.push(goods);
}
var show = document.getElementById("des");
show.innerText = list;
</script>
答案 1 :(得分:0)
var howMany = prompt("how many do you want to buy?");
var goods = "";
var list = [];
for (var i = 0 ; i < howMany ; i++){
goods = prompt("add a name ?");
list.push(goods);
}
var show = document.getElementById("des");
show.innerText = list;
<div id="des"></div>
试试这个