如何删除CreateElement和AppendChild创建的元素

时间:2018-03-14 08:02:13

标签: javascript html removechild

[



// Calendar
var calendar = document.getElementById("calendar");

calendar.addEventListener("click",function(){

    factbox.style.bottom = "70px";
    factbox.style.transition = "0s";    
    calendar.style.transform = "rotateX(-50deg)";
    calendar.style.transformOrigin = "bottom center";
        
    var hotdogs = document.createElement("IMG");
    hotdogs.src = "SVG/hotdog2.svg";
    hotdogs.setAttribute = ("id", "hotdogs");
    everywhere.parentNode.removeChild("hotdogs");
});

// Hotdog
var hotdog = document.getElementById("hotdog");
var everywhere = document.getElementById("everywhere");
var NumClick4 = 0;
hotdog.addEventListener("click",function(){
    
    var hotdogs = document.createElement("IMG");
    hotdogs.src = "SVG/hotdog2.svg";
    hotdogs.setAttribute = ("id", "hotdogs");
    everywhere.appendChild(hotdogs);
    var x = Math.floor(( Math.random() * 450) + 800);
    
    NumClick4++;
    console.log(x + " " + NumClick4);
    hotdogs.style.width = "200px";
    hotdogs.style.left = x + "px";
    if(NumClick4 == 1) {
    
    factbox.style.bottom = "70px";
    factbox.style.transition = "0s";

    } else {
             
    }
});

<div id="hotdog" class="">
                <img src="SVG/hotdog2.svg" alt="hotdog">
            </div>
            <div id="everywhere"></div>
   
            <div id="calendar">
                <img src="SVG/calendar.svg" alt="calendar">
            </div>
            
            <div id="factbox">
            </div>
&#13;
&#13;
&#13;

enter image description here 对不起,如果我的代码看起来很乱。因此,如图所示,我想要天空中的热狗,这些热狗是由&#34; CreateElement&#34;和&#34; AppendChild&#34;使用&#34; RemoveChild&#34;消失的方法方法。例如,当我点击其他元素如&#34; flag&#34;时,我想要的只是新的&#34;热狗&#34;消失我想我不知道它是如何运作的。我很感激建议和提示。

1 个答案:

答案 0 :(得分:0)

以下行是错误的2次计数

everywhere.parentNode.removeChild("hotdogs");
  • 您正在尝试删除尚未添加的内容

  • 您需要删除Element而不是string

everywhere.parentNode.removeChild(hotdogs);