【js】appendChild
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>
【js】appendChild的更多相关文章
- 【JS】AJAX跨域-被调用方与调用方解决方案(二)
解决跨域问题 跨域问题说明,参考[JS]AJAX跨域-JSONP解决方案(一) 实例,使用上一章([JS]AJAX跨域-JSONP解决方案(一))的实例 解决方案三(被调用方支持跨域-服务端代码解决) ...
- 【js】Leetcode每日一题-制作m束花所需的最少天数
[js]Leetcode每日一题-制作m束花所需的最少天数 [题目描述] 给你一个整数数组 bloomDay,以及两个整数 m 和 k . 现需要制作 m 束花.制作花束时,需要使用花园中 相邻的 k ...
- 【js】Leetcode每日一题-完成所有工作的最短时间
[js]Leetcode每日一题-完成所有工作的最短时间 [题目描述] 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间. 请你将这些工作分配给 k 位工人.所有工 ...
- 【js】Leetcode每日一题-数组异或操作
[js]Leetcode每日一题-数组异或操作 [题目描述] 给你两个整数,n 和 start . 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == ...
- 【js】Leetcode每日一题-解码异或后数组
[js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...
- 【js】Leetcode每日一题-叶子相似的树
[js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...
- 【js】Leetcode每日一题-子数组异或查询
[js]Leetcode每日一题-子数组异或查询 [题目描述] 有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]. 对于每个查询 i ...
- 【js】Leetcode每日一题-停在原地的方案数
[js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...
- 【js】Leetcode每日一题-二叉树的堂兄弟节点
[js]Leetcode每日一题-二叉树的堂兄弟节点 [题目描述] 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处. 如果二叉树的两个节点深度相同,但 父节点不 ...
随机推荐
- 遇到问题描述:Android Please ensure that adb is correctly located at问题解决
遇到问题描述: 运行android程序控制台输出 [2013-11-04 16:18:26 - ] The connection to adb is down, and a severe error ...
- idea 创建 简单的scala maven项目
1.创建maven scala项目 2.maven配置 <properties> <scala.version>2.11.8</scala.version> < ...
- 我为何放弃Gulp与Grunt,转投npm scripts(上)
本文来源于我在InfoQ中文站翻译的文章.原文地址是:http://www.infoq.com/cn/news/2016/02/gulp-grunt-npm-scripts-part1 Cory Ho ...
- 【C#】C#委托学习
虽然做.NET这行也快2年了,但基础不太好啊,今天看了下委托方面的知识,记录下. 1.委托 总的来说,委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋 ...
- 学习Struts框架系列(三):声明式异常处理
在Struts1.X的版本中加入了对异常的处理Exception Handler,有了它我们可以不使用try/catch捕获异常,一旦出现了我们已经定义的异常,那么就会转到相应的页面,并且携带异常信息 ...
- Facebook 开源动画库 pop
官网:https://github.com/facebook/pop Demo: https://github.com/callmeed/pop-playground 一:pop的基本构成: POPP ...
- 微信小程序 - 各种示例
示例更新(后续持续更新): 最后一次更新:2018-11-7 小程序-地图显示(调用高德地图.腾讯App) - (2018-11-1) 小程序-上传图片(上传.预览.展示.删除) - (2018-11 ...
- Java从零开始学十四(包和访问控制)
一.java中的包 Java文件的组织形式Windows中的文件功能类似 在开发比较大的项目时,不可能只涉及到一个java文件,可能要创建几十,甚至几百个java文件,这个时候,我们就可以使用包,把相 ...
- Windows平台Hadoop编译、安装、配置与运行(转)
http://www.srccodes.com/p/article/38/build-install-configure-run-apache-hadoop-2.2.0-microsoft-windo ...
- C和C++静态检查规范