A Web UI plug-in was recently used, but its global variables were used for calling,
That is to say, a page can only be used once. This plug-in is quite useful, but I need more than two pages.
This plug-in makes me think of JS object cloning.
Idea: clone an object (not a reference, reference or the same object), and then
Change the key attributes (such as ID) of the new object to two different objects.
I can modify these two objects on the page without affecting them.
Unfortunately, JS does not have a ready-made clone method. You need to write it yourself.
Some people have written such a method on the Internet, but there may be problems because recursion is required.
I tried it and it was really a problem and it was useless.
However, jquery's extend can be implemented, which has been tested and recorded today.
// Perform shallow copy (only copy the top-level non-object elements)
// Var newobject = jquery. Extend ({}, oldobject );
// Deep replication (one layer to the bottom)
// Var newobject = jquery. Extend (true, {}, oldobject );
Function clone (o) {If (! O) {return O;} else {var C; If (object. prototype. tostring. apply (o) = '[object array]') {c = []; for (VAR I = 0; I <O. length; I ++) {C. push (clone (O [I]);} // use this judgment, instead of typeof (o) = 'object'. Here, only the object is processed, // do not process other functions, including array, String, date, and function. However, the function object created by new is also an object} else if (object. prototype. tostring. call (o) = '[object]') {c = {}; for (var p in O) {c [p] = clone (O [p]) ;}} else {return O ;} Return C ;}} function clonedd (myobj) {If (typeof (myobj )! = 'Object') return myobj; If (myobj = NULL) return myobj; var mynewobj = new object (); For (var I in myobj) mynewobj [I] = clonedd (myobj [I]); Return mynewobj;} function MyClone (OBJ) {var arr1 = new array (); arr1.push (OBJ ); vaR arr2 = []. concat (arr1); var res = arr2 [0]; return res;} // shallow copy (only copying top-level non-object elements) // var newobject = jquery. extend ({}, oldobject); // deep replication (one layer to the bottom) // var newobject = jquery. extend (true, {}, oldobject );
The following figure shows how to use the global variable navtab,
Copy this variable to get A, then change its attribute ID, and OK to complete the replication.
$ (Function () {navtab. init ({ID: "navtab"}); var A = jquery. extend (true, {}, navtab);. init ({ID: "navtab2 "});});