OldObj is an object, not a value
For example, var newObj = oldObj;
If you want to change the oldObj, newObj is not affected;
You can write a function like this.
Copy codeThe Code is as follows: function clone (myObj ){
If (typeof (myObj )! = 'Object') return myObj;
If (myObj = null) return myObj;
Var myNewObj = new Object ();
For (var I in myObj)
MyNewObj [I] = clone (myObj [I]);
Return myNewObj;
}
Then, call
NewObj = clone (oldObj)
After that, the value of oldObj will not affect newObj no matter how it is changed.
JavaScript copying objects
Syntax:
OElement = object. cloneNode (bCloneChildren)
Parameters:
BCloneChildren: Optional. Boolean ). False | true
False: default value. When cloning an object, the childNodes set of the object is not included. That is, all its sub-objects.
True: When cloning an object, it includes the childNodes set of the object. That is, all its sub-objects.
Return Value:
OElement: Object (Element ). Returns the reference of the new object after cloning.
Note:
Clone an object in the document structure.
After cloning, a collection is returned when the id of the cloned object is obtained.
You can use this method at runtime. Before the close tag of an object is resolved, the file space may not be presented.
Sample Code:Copy codeThe Code is as follows: <script>
Function rdl_fnClone (){
Var oCloneNode = oList. cloneNode (true );
CloneArea. appendChild (oCloneNode );
}
</Script>
<Table width = 98%> <tr> <td>
<Ul id = oList>
<Li> 1st list items
<Li> 2nd <a href = "#" onclick = "return false;"> list items </a>
<Li style = "font-weight: bold;"> 3rd list items
</Ul>
</Td> <td id = cloneArea>
</Td> </tr> </table>
<Input type = button value = "clone" onclick = "rdl_fnClone ();">