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的更多相关文章

  1. 【JS】AJAX跨域-被调用方与调用方解决方案(二)

    解决跨域问题 跨域问题说明,参考[JS]AJAX跨域-JSONP解决方案(一) 实例,使用上一章([JS]AJAX跨域-JSONP解决方案(一))的实例 解决方案三(被调用方支持跨域-服务端代码解决) ...

  2. 【js】Leetcode每日一题-制作m束花所需的最少天数

    [js]Leetcode每日一题-制作m束花所需的最少天数 [题目描述] 给你一个整数数组 bloomDay,以及两个整数 m 和 k . 现需要制作 m 束花.制作花束时,需要使用花园中 相邻的 k ...

  3. 【js】Leetcode每日一题-完成所有工作的最短时间

    [js]Leetcode每日一题-完成所有工作的最短时间 [题目描述] 给你一个整数数组 jobs ,其中 jobs[i] 是完成第 i 项工作要花费的时间. 请你将这些工作分配给 k 位工人.所有工 ...

  4. 【js】Leetcode每日一题-数组异或操作

    [js]Leetcode每日一题-数组异或操作 [题目描述] 给你两个整数,n 和 start . 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == ...

  5. 【js】Leetcode每日一题-解码异或后数组

    [js]Leetcode每日一题-解码异或后数组 [题目描述] 未知 整数数组 arr 由 n 个非负整数组成. 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encode ...

  6. 【js】Leetcode每日一题-叶子相似的树

    [js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...

  7. 【js】Leetcode每日一题-子数组异或查询

    [js]Leetcode每日一题-子数组异或查询 [题目描述] 有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]. 对于每个查询 i ...

  8. 【js】Leetcode每日一题-停在原地的方案数

    [js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...

  9. 【js】Leetcode每日一题-二叉树的堂兄弟节点

    [js]Leetcode每日一题-二叉树的堂兄弟节点 [题目描述] 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处. 如果二叉树的两个节点深度相同,但 父节点不 ...

随机推荐

  1. Objective-C:保留计数器思想的详解(对象的保留和所有权的释放)

    对象的保留和所有权的释放: int main(int agrs,char *argv[]) { @autoreleasepool{ Person *person = [[Person alloc]in ...

  2. IIS HTTP 错误 404.17 - Not Found 解决方法

    错误提示如下图: 出现这种情况的原因通常是因为先安装了Framework,后安装的IIS: 运行cmd,输入:   C:\Windows\Microsoft.NET\Framework\V4.0.30 ...

  3. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  4. 基于Java spring框架的微信企业号开发中关于js-sdk的配置

    在调用js-sdk的第一步,我们需要引入js-sdk的js链接,然后执行wx.config,官方示例如下所示: wx.config({ debug: true, // 开启调试模式,调用的所有api的 ...

  5. cognos report在做同比时遇到的问题解决方法

    本例就拿简单的一个模型作为测试: 订单中包括日期key,商品类型key 现在要实现每月的订单数,以及去年同期的订单数: step1:新建2个数据项 本月,去年同月 本月: [每日订单数据分析].[日期 ...

  6. Linux shell 脚本入门教程+实例

    原文:http://www.wiquan.com/article/136 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是shell仍然是一个非常灵活的工具.She ...

  7. cygwin64安装wget和apt-cyg

    说实话,网上的教程要么不适用,要么不能用,唯有多次试错之后才杀出一条血路. 1.安装cygwin 2.勾选wget 这个如果忘记勾选了,就再次打开setup.exe,选择如下操作,即可. 然后点击下一 ...

  8. [HTML5] Using the focus event to improve navigation accessibility (nextElementSibling)

    For a menu item, when we tab onto it, we want this element get 'focus' event, so that the submenu wi ...

  9. Windows Server 2012 R2 或 2016 无法安装 .Net 3.5.1

    Windows Server 2012 R2 或 2016 无法安装 .Net 3.5.1 问题描述 使用 Windows Server 2012 R2 或 Windows Server 2016系统 ...

  10. uva 10721 - Bar Codes(dp)

    题目链接:uva 10721 - Bar Codes 题目大意:给出n,k和m,用k个1~m的数组成n,问有几种组成方法. 解题思路:简单dp,cnt[i][j]表示用i个数组成j, cnt[i][j ...