replacing div content with javascript

后端 未结 6 2168
悲&欢浪女
悲&欢浪女 2020-12-09 01:48

I\'m trying to do this with pure javascript, not with jquery. I have a div that has an id test and contains other divs inside it. How do I empty the content of

相关标签:
6条回答
  • 2020-12-09 01:54

    clear the div:

    document.getElementById('test').innerHTML = '';
    

    replace it :

    var h1 = document.createElement('h1');
    h1.innerHTML = "hello world!";
    document.getElementById('test').appendChild(h1);
    
    0 讨论(0)
  • 2020-12-09 02:00
    document.getElementById("test").innerHTML = "new content"
    
    0 讨论(0)
  • 2020-12-09 02:00
    document.getElementById("test").innerHTML = "Some other Content";
    
    0 讨论(0)
  • 2020-12-09 02:02
    var testDiv = document.getElementById("test");
    testDiv.innerHTML = "<h5>It works!</h5>";
    
    0 讨论(0)
  • 2020-12-09 02:08

    Use document.getElementById('test').innerHTML = ''.

    0 讨论(0)
  • 2020-12-09 02:11

    Off the top of my head, I think this should work....

    document.getElementById("test").innerHTML = "";
    
    0 讨论(0)
提交回复
热议问题