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:
]
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.
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.
[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>