简体   繁体   中英

append a html file to div created by a js script

I am trying to append an HTML file to a div recent created by a js script but I get an error in the jquery library,

the code here, create a div and I want to append room.html within:

var contenedor = $('PageLoaded');
$('#button').click(function(){
    container = document.createElement('<div></div>');
    container.setAttribute('class','container');
    container.get("room.html", function(htmlexterno){
       $(".container").html(htmlexterno);
    });

How can I embed room.html into the div which I am creating?

You seem a little confused about the difference between native JS and jQuery methods.

container is an Element object. It has no get() method as that's part of jQuery. To fix this you could use $.get() instead.

However, given your usage, it would appear that $.load() is more appropriate here as your goal is to add content to the new element.

$('#button').click(function(){
  let $container = $('<div class="container"></div>').appendTo('#yourSelectorHere...');
  $container.load("room.html");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM