cloneNode和insertBefore 的用法
function fnClone(){
/* the 'true' possible value specifies to clone
the childNodes as well.
*/
var oCloneNode = oList.cloneNode(true);
/* When the cloned node is added,
'oList' becomes a collection.
*/
document.body.insertBefore(oCloneNode);
}
</SCRIPT>
<UL ID="oList">
<LI>List node 1
<LI>List node 2
<LI>List node 3
<LI>List node 4
</UL>
<INPUT type="button" value="Clone List" onclick="fnClone()">
Syntax
oClone = object.cloneNode( [bCloneChildren])
Parameters
bCloneChildren Optional. Boolean that specifies one of the following values:
false Default. Cloned objects do not include childNodes. true Cloned objects include childNodes.
Return Value
Returns a reference to the newly created node.
insertBefore : 在文档层次中插入元素。
Syntax (
syntax: [ 'sintæks ] . . )
n. 句法
oElement = object.insertBefore(oNewNode [, oChildNode])
Parameters (参数)
oNewNode Required. Object that specifies the new element to be inserted into the document hierarchy. Elements can be created with the createElement method.
(需要。 对象,它指定新元素插入到文档层次结构。 可以使用 createElement 方法创建元素。)
oChildNode Optional. Object that specifies the placement of the new element. If this parameter is specified, the new element will be inserted immediately before this existing child element.
(可选项。 对象,它指定新的元素的位置。 如果指定此参数,新的元素将被插入此现有的子元素前面。)
Return Value
Returns a reference to the element that is inserted into the document.
(返回一个对插入到文档的元素的引用。)
Example
The following example shows how to use the insertBefore method to insert a new item into an existing list.(下面的示例演示如何使用 insertBefore 方法向现有列表中插入一个新的项)
<SCRIPT>
function insertElement()
{
var nod=document.createElement("LI");
oUL1.insertBefore(nod, oLIYellow);
nod.innerText="Orange";
}
</SCRIPT>
</HEAD>
<BODY>
<SPAN onclick=insertElement()>Click <B>HERE</B> to add an item to the following list.</SPAN>
<UL id="oUL1">
<LI id="oLIRed">Red</LI>
<LI id="oLIYellow">Yellow</LI>
<LI id="oLIBlue">Blue</LI>
</UL>
</BODY>