Home Web Front-end JS Tutorial By element.appendChild(newNode), let's talk about _javascript skills

By element.appendChild(newNode), let's talk about _javascript skills

May 16, 2016 pm 07:23 PM

In the element.appendChild(newNode) method, if newNode itself is a node in Dom, then the appendChild method performs an append operation no longer
, but a move operation. For example:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Because btn1 itself is in dom A node, so the appendChild operation will move btn1 to the back of btn4 instead of copying it.
Using this feature, we can achieve alternative text seamless scrolling with a very small amount of code.

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Haha, isn’t it very simple? Well, I posted this method on CSDN in May this year, and it aroused a lot of discussion among JavaScript enthusiasts
. The post was pinned to the top of the CSDN homepage, and 300 people participated in replying to the discussion.
In addition to this application, this feature of appendChild can also be used in table sorting.
Note that the following table sorting code is only to illustrate the usage of appendChild and has not been tested in ff.
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

How about appendChild? With great help, this sorting is simple enough.
For more table operation methods and advanced custom table sorting, please refer to my other post on CSDN (the sorting is also implemented using appendChild and is compatible with firefox)
<script> function f(){ document.body.appendChild(document.getElementById("btn1")) } </script><script> // by Go_Rush(阿舜) 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><script> // by Go_Rush(阿舜) from http://ashun.cnblogs.com/ function $A(arrayLike){ for(var i=0,ret=[];i<arrayLike.length;i++) ret.push(arrayLike[i]); return ret } Array.prototype.each=function(f){for(var i=0;i<this.length;i++) f(this[i],i,this)} window.onload=function(){ $A(document.getElementById("tbl").rows).sort(function(tr1,tr2){ return Number(tr1.cells[0].innerHTML)>Number(tr2.cells[0].innerHTML)?1:-1 }).each(function(tr){ document.getElementById("tbl").firstChild.appendChild(tr) }) } </script>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles