我正在創(chuàng)建一個函數(shù),當(dāng)單擊 html 按鈕時創(chuàng)建圖像,如果再次單擊按鈕,它會刪除創(chuàng)建的圖像。當(dāng)我再次單擊按鈕時它不會刪除。任何幫助,將不勝感激。 a = 0; function createimg() { var newElement = document.createElement("img"); newElement.setAttribute("id", "img"); newElement.setAttribute("src", "shield.png"); var list = document.getElementById("img"); if (a == 0) { document.body.appendChild(newElement); a = 1; } else document.body.removeChild(newElement); }
創(chuàng)建元素然后移除元素
翻翻過去那場雪
2021-12-23 15:57:31