insertBefore小记

in 未归类 with 0 comments, 1108 views

时间一长,此方法的参数又忘了,顺便恶补一下。

parentElement.insertBefore( newElement, targetElement )

其中:

parentElement为想要插入位置的父(元素)结点,新元素将在其内插入;

newElement为待插入的新元素;

targetElement为插入位置的参考结点,即新元素将插在此结点之前;

html:

<li id="oNewElement" style='color:#3366ff'>newElement</li>
<ul id="oParentElement" style='color:#993300'>parentElement
	<li id="oTargetElement" style='color:#008000'>targetElement</li>
</ul>

javascript:

var oParentElement = document.getElementById('oParentElement');
var oTargetElement = document.getElementById('oTargetElement');
var oNewElement = document.getElementById('oNewElement');
oParentElement.insertBefore(oNewElement, oTargetElement)

运行结果:


Responses ${replyToWho} / Cancel Reply