當試圖追加到我的列表中時,它只是將它添加到同一列表項的末尾。這裏是我的JS:追加到javascript中的列表
var title = document.createElement("h1");
var subTitle = document.createElement("h3");
var img = document.createElement("img");
var list = document.createElement("li");
var btn = document.createElement("button");
var myDiv = document.getElementById("container");
var myList = document.getElementById("pokemon");
var keepGoing = true;
function pokemonList(){
var theList = prompt("Enter one of your favorite pokemon");
list.textContent = theList;
myList.appendChild(list);
keepGoing = confirm("Would you like to add another pokemon?");
if(keepGoing)
pokemonList();
}
<body>
<div id = "container"></div>
<ol id="pokemon"></ol>
<script src="pageGenerator.js" type="text/javascript"></script>
<script>myPage();</script>
<script>pokemonList();</script>
</body>
編輯:第一個問題是固定的,但現在的第一個元素被替換每次我在一個新的口袋妖怪進入。
請包括一個最小**完整的例子。您發佈的代碼缺少幾個關鍵部分(如創建「list」變量),導致無法運行。 – meagar