In the element. appendChild (newNode) method, if newNode itself is a node in the Dom, The appendChild method is no longer executed.
It is an append operation, but a move operation. For example:
<Html> script function f () {document. body. appendChild (document. getElementById ("btn1")} script <body onload = f ()> <input type = button id = btn1 value = "button 1"> <input type = button id = btn2 value = "button 2"> <input type = button id = btn3 value = "button 3"> <input type = button id = btn4 value = "button 4"> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Because btn1 itself is a node in the dom, The appendChild operation will move btn1 to the end of btn4.
With this feature, we can use a very small amount of code to achieve seamless scrolling of alternative words.
<Style> * {border: 1px solid green} a {display: block; font-size: 15px };</style> <p id = "d" style = "width: px; height: Px; "> 1. You may have had countless dreams. 2. However, you may lose sight in the passage of time. 3. You may look forward to the future. 4, but lost in the years of growth </p> script // by Go_Rush (A Shun) from http://ashun.cnblogs.com/var t = setInterval (myfunc, 1000) function myfunc () {d. appendChild (d. firstChild)} d. onmouseover = function () {clearInterval (t)} d. onmouseout = function () {t = setInterval (myfunc, 1000)} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Haha, isn't it very concise? I sent this method to CSDN in September, which caused a lot of JavaScript hobbies.
The post was topped by the CSDN homepage, and three hundred people attended the discussion.
In addition to this application, this feature of appendChild can also be used in table sorting.
Note: The following table sorting code is only used to demonstrate appendChild usage and is not tested in ff.
<Table border id = "tbl" width = "40%"> <tr> <td> 12 </td> <td> 1 </td> </tr> <tr> <td> 9 </td> <td> 2 </td> </tr> <td> 15 </td> <td> 3 </td> </tr> <td> 18 </td> <td> 4 </td> </tr> <td> 1 </td> <td> 5 </td> </tr> </table> script // by Go_Rush (A Shun) from http://ashun.cnblogs.com/function $ A (arrayLike) {for (var I = 0, ret = []; iNumber (tr2.cells [0]. innerHTML )? 1:-1}). each (function (tr) {document. getElementById ("tbl"). firstChild. appendChild (tr)} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
With the help of appendChild, the sorting is simple enough.
For more table operations and advanced custom table sorting methods, see my other post on CSDN (also sorted by appendChild and compatible with firefox)