简体   繁体   中英

innerHTML inserts only [object HTMLDivElement]

i want to insert a htmlsnippet into another html-element

i tried it this way

the html

<div class="box1">
    insert this html element
</div>
<div class="box2">
  into this
</div>

the js

var box1 = document.querySelectorAll(".box1")[0]; 
var box2 = document.querySelectorAll(".box2")[0];
console.log(box1);
box2.innerHTML = box1;

but it doesnt work it only insert [object HTMLDivElement], if i look at the console it puts out the correct html, what im doing it wrong?

and yeah i dont want to use the $ libary ;)

http://codepen.io/destroy90210/pen/MKQOwy

thanks ;)

innerHTML doesn't insert DOM nodes, just strings.
Use appendChild instead

var box1 = document.querySelector(".box1"); 
var box2 = document.querySelector(".box2");

box2.appendChild( box1 );

您应该使用appendChild方法。

box2.appendChild(box1);

这应该工作:

box2.appendChild(box1);

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