日期:2014-05-16  浏览次数:20381 次

请问cloneNode,previousSibling 这个是怎么用的。
<!--为什么下面这段代码不会一个一个复制而是全部复制 求解 -->
<HTML>
  <HEAD>
    <TITLE>   New   Document   </TITLE>
<SCRIPT   LANGUAGE= "JavaScript ">
  <!--
function   clone(obj){
var   newDiv=obj.cloneNode(true);
obj.insertBefore(newDiv);
}

  //-->
  </SCRIPT>

  </HEAD>

  <BODY>

<FORM   METHOD=POST   ACTION= " ">
    <div   style= "height:20px "   align= "center "> xxx <span> aaa <span> </div>
    <INPUT   TYPE= "button "   value= "clone "   onclick= "clone(this.previousSibling) ">
</FORM>
  </BODY>
</HTML>


------解决方案--------------------
Node.cloneNode( ) duplicate a node and, optionally, all of its descendants


Availability

DOM Level 1 Core
Synopsis
Node cloneNode(boolean deep);
Arguments
deep

If this argument is true, cloneNode( ) recursively clones all descendants of this node. Otherwise, it clones only this node.
Returns

A copy of this node.
Description

The cloneNode( ) method makes and returns a copy of the node on which it is called. If passed the argument true, it recursively clones all descendants of the node as well. Otherwise, it clones only the node and none of its children. The returned node is not part of the document tree, and its parentNode property is null. When an Element node is cloned, all of its attributes are also cloned. Note, however, that EventListener functions registered on a node are not clone