![](/img/trans.png)
[英]Get the innerHTML of [object XrayWrapper [object HTMLDivElement]] object
[英]innerHTML inserts only [object HTMLDivElement]
我想将htmlsnippet插入另一个html元素
我这样尝试过
HTML
<div class="box1">
insert this html element
</div>
<div class="box2">
into this
</div>
js
var box1 = document.querySelectorAll(".box1")[0];
var box2 = document.querySelectorAll(".box2")[0];
console.log(box1);
box2.innerHTML = box1;
但是它不起作用,它只能插入[object HTMLDivElement],如果我在控制台中看到正确的html,那我在做什么错呢?
是的,我不想使用$ libary;)
http://codepen.io/destroy90210/pen/MKQOwy
谢谢 ;)
innerHTML
不会插入DOM节点,而只会插入字符串。
使用appendChild
代替
var box1 = document.querySelector(".box1");
var box2 = document.querySelector(".box2");
box2.appendChild( box1 );
您应该使用appendChild方法。
box2.appendChild(box1);
这应该工作:
box2.appendChild(box1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.