appendChild()常用功能。
- 平时我们用appendChild的时候,都是向父级上添加子元素
- appendChild的另一个功能是,先把元素从原有父级上删掉,然后添加元素到新的父级。(移除再添加)。
代码说明
<!DOCTYPE html> <html> <head> <title>appendChild的第二种功能</title> <script> window.onload=function(){ var oUl1=document.getElementById("ul1"); var oUl2=document.getElementById("ul2"); var oBtn=document.getElementById("btn1"); oBtn.onclick=function(){ var oLi=oUl1.children[0]; oUl1.appendChild(oLi); } } </script> </head> <body> <ul id="ul1"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <input type="button" id="btn1" value="移动"> </body> </html>
用appendChild的第二种功能实现一个li按照内容大小排序
<!DOCTYPE html> <html> <head> <title>appendChild的第二种功能</title> <script> window.onload=function(){ var oUl1=document.getElementById("ul1"); var oBtn=document.getElementById("btn1"); oBtn.onclick=function(){ var aLi=oUl1.getElementsByTagName("li"); // aLi是一个元素集合,不是真正意义的数组,不能用sort方法,转成数组再用sort排序 var arr=[]; for(var i=0; i<aLi.length; i++){ arr.push(aLi[i]); } arr.sort(function(li1,li2){ var n1=parseInt(li1.innerHTML); var n2=parseInt(li2.innerHTML); return n1-n2 }); for(var j=0; j<arr.length; j++){ oUl1.appendChild(arr[j]);//当添加子元素的时候以前的元素已经被删除,所以不会出现重复元素 } } } </script> </head> <body> <ul id="ul1"> <li>12</li> <li>2</li> <li>30</li> <li>22</li> </ul> <input type="button" id="btn1" value="移动"> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 王菲.2006《菲常好听》引进版[低速原抓WAV+CUE]
- 《三日危情独家的记忆》女角色介绍
- 《天国拯救》拉泰比武大会攻略
- 任天堂推出《马里奥惊奇》NS捆绑包 10月24日发售
- 《孤岛惊魂》能否逆转育碧颓势?曝系列新作正招兵买马
- 天命人呼叫战术轰炸!玩家自制《黑神话:悟空》轰炸机Mod
- 群星.1992-滚石九大天王·十二出好戏【滚石】【WAV+CUE】
- 群星.1994-新滚石九大天王之年度大丫滚石】【WAV+CUE】
- 坣娜.1998-别恋【非常喜】【WAV+CUE】
- 《草原经典 最炫草原风 2CD》[WAV/分轨][980MB]
- 《不烧不选 经典国语极品珍藏》[WAV/分轨][500MB]
- 《绝对主打 流行原创金曲 2CD》[WAV分轨][1.1GB]
- 《赛博朋克2077》更新上线:支持AMD FSR3
- 《塞尔达传说:智慧的再现》提前半个月偷跑,引发玩家担忧
- 战锤40K星际战士2全职业介绍|全职业技能效果一览