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

WBOY
Release: 2016-05-16 19:23:44
Original
1096 people have browsed it

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>
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template