我想在父节点内的2个子元素之间添加一个元素。以下是HTML:
HTML:
<div class="parent">
<div class="child1"></div>
// I want to insert <span class="child2"></span> here
<div class="child3"></div>
</div>
我试过了:
var newNode = document.createElement("span");
var a = Ext.select("child3")
Ext.select("parent").insertBefore(newNode,a );
这没有做任何事情。想法如何解决这个问题?
谢谢!
答案 0 :(得分:1)
var referenceNode=document.body.querySelector('.child1'),
newNode=document.createElement('span');
newNode.innerHTML='child2';
newNode.setAttribute('class','child2');
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
&#13;
<div class="parent">
<div class="child1">child1</div>
<div class="child3">child3</div>
</div>
&#13;