How do I append HTML from a document loaded with AJAX?

前端 未结 6 1474
闹比i
闹比i 2021-01-13 19:13

I am trying to append the contents of a .html file to the body of my main page. Basically, I am trying to make a reusable chunk of html that I can load into any page with a

6条回答
  •  一向
    一向 (楼主)
    2021-01-13 19:35

    I assume you can create a div and then modify the div.innerHTML to have the content of the response:

    function importHTML(url_) {
      var request = new XMLHttpRequest();
    
      request.addEventListener("load", function(event_) {
    
        var myDiv = document.createElement("div")
        myDiv.innerHTML = this.responseText
    
        document.body.appendChild(myDiv);
      }, false);
    
      xmlhttprequest.open("POST", url_, true);
      xmlhttprequest.send(null);
    }
    

提交回复
热议问题