appendChild主要是用来追加节点插入到最后;循环的时候由于不停的搬家导致length在改变。

    使用for循环

<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link REL="SHORTCUT ICON" HREF="favicon.ico" type="image/x-icon" />
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta http-equiv=X-UA-Compatible content=IE=EmulateIE7>
<title>【js】appendChild </title>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload = function(){
var ul2 = document.getElementById('ul2');
var oli = document.getElementById('ul1').children;
for(var i=0;i<oli.length;i++){
//alert("former:"+oli.length);
ul2.appendChild(oli[i]);
//alert("latter:"+oli.length);
}
}
</script>
</head>
<body> <h3>将Id为ul1的内容插入到ul2里面</h3>
<ul id="ul1">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<ul id="ul2">
<li>ul2</li>
</ul>
</body>
</html>

运行效果图:

使用while循环

window.onload = function(){
var ul2 = document.getElementById('ul2');
var oli = document.getElementById('ul1').children;
while(oli.length){
//alert("former:"+oli.length);
ul2.appendChild(oli[0]);
//alert("latter:"+oli.length);
}
}

运行效果图:

while和for循环差生不同结果的原因:

是因为循环的时候,由于不停的搬家导致数组在改变,在用for循环的时候,每次appendChild()的时候,数组减少,但是变量i却还在增加,所以导致出现效果一的情况;而while循环的时候就不一样了,该循环可以取出数组中的全部内容。

例子:左右列表选择的js实现:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xml="http://www.w3.org/1999/xhtml">
<head>
<title>Select Page</title>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta http-equiv=X-UA-Compatible content=IE=EmulateIE7>
<style type="text/css">
* {font-family:Tahoma,Arial,serif;font-size:11pt;line-height:25px;}
body {text-align:center;min-width:760px;}
#main {width:720px;margin:0 auto;text-align:left;}
#main div {width:30%;display:inline;}
#main div input {position:absolute;left:500px;}
p {text-indent:2em;}
select {width:120px;}
</style>
<script type="text/javascript">
//右移
function moveRight(){
//左侧列表框
var leftSel=$("left");
//右侧列表框
var rightSel=$("right");
//左侧列表框的选项集合
var options=leftSel.options;
//遍历所有的选中的选项
for(var i=0;i<options.length;i++){
//选中项
if(options[i].selected){
//将选项移动到右侧列表框中
rightSel.appendChild(options[i]);
i--;
}
}
}
function $(id){
return document.getElementById(id);
}
//左移
function moveLeft(){
//左侧列表框
var leftSel=$("left");
//右侧列表框
var rightSel=$("right");
//右侧列表框的选项集合
var options=rightSel.options;
//遍历所有的选中的选项
for(var i=0;i<options.length;i++){
//选中项
if(options[i].selected){
//将选项移动到左侧列表框中
leftSel.appendChild(options[i]);
i--;
}
}
}
//全部右移
function moveRightAll(){
//左侧列表
var leftSel=$("left");
//右侧列表
var rightSel=$("right");
//将所有左侧选项移动到右侧
while(leftSel.options.length>0){
rightSel.appendChild(leftSel.options[0]);
}
}
//全部左移
function moveLeftAll(){
//左侧列表
var leftSel=$("left");
//右侧列表
var rightSel=$("right");
//将所有右侧选项移动到左侧
while(rightSel.options.length>0){
leftSel.appendChild(rightSel.options[0]);
}
}
</script>
</head>
<body>
<div id="main">
<div>
<select id="left" size="10" multiple="multiple">
<option value="a">选项A</option>
<option value="b">选项B</option>
<option value="c">选项C</option>
<option value="d">选项D</option>
<option value="e">选项E</option>
<option value="f">选项F</option>
</select>
</div>
<div>
<input type="button" value="右移" style="top:20px;" onclick="moveRight()"/>
<input type="button" value="左移" style="top:70px;" onclick="moveLeft()"/>
<input type="button" value="全部右移" style="top:120px;" onclick="moveRightAll()"/>
<input type="button" value="全部左移" style="top:170px;" onclick="moveLeftAll()"/>
</div>
<div style="left:100px;position:relative;">
<div>
<select id="right" size="10" multiple="multiple">
</select>
</div>
</div>
</div>
</body>
</html>

最新文章

  1. linkedin开源的kafka-monitor安装文档
  2. Head First HTML5 Programming 读书笔记
  3. 官网下载jdk
  4. iOS 7 如何关闭已打开的应用(App)
  5. Tarjan+模板
  6. 【5】图解HTTP 笔记
  7. fopen()函数以&quot;a+&quot;方式打开一个不存在的文件后读写出现问题
  8. Sql的基础知识技巧(三)
  9. 编写输出“Hello world”
  10. @getMapping与@postMapping
  11. Django---请求、响应
  12. MySql5.7 Distinct与Order By同时使用报错的解决方案
  13. German Collegiate Programming Contest 2018​ A. Attack on Alpha-Zet
  14. mysql学习之路_基础知识
  15. 用《舌尖2》去理解C#中的多态和开闭原则
  16. http://www.gasi.ch/blog/inside-deep-zoom-1/
  17. VMware安装与VMware下安装CentOS系统
  18. redis list 查询、下标查询、删除、裁剪、压入弹出、队列实现
  19. 关于几个与IO相关的重要概念
  20. Android ListView根据项数的大小自动改变高度

热门文章

  1. 第一章 Java代码执行流程
  2. 什么是ScaleIO中的forwards rebuild和backwards rebuild?
  3. 使用FlexiGrid实现Extjs表格的效果-网络传输小,更方便!
  4. eclipse全屏模式
  5. Java中看今天是星期几,礼拜几
  6. (算法)Partition方法求数组第k大的数
  7. 帝吧fb出征是什么原因?帝吧fb出征事情始末 帝吧出征FB打“台独” 台湾网民崩溃:巨人之墙爆了
  8. miniupnpc
  9. poj 1879 Truck History
  10. SSH框架学习