By element. appendChild (newNode), we will talk about the _ javascript Technique

Source: Internet
Author: User
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)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.