<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title> <style type="text/css">
.box1,
.box2{
width: 300px;
height: 250px;
margin-top: 10px;
margin-bottom: 30px;
border: 1px solid green;
} .box1 > div,
.box2 > div{
border: 1px solid red;
margin: 5px;
padding: 10px 5px;
}
</style> <script type="text/javascript">
//测试创建元素
function testCreate(){
var div = document.createElement("div");
div.innerHTML = "this is a new div";
div.style.color = "green";
var target = document.querySelector('.box1');
var target2 = document.querySelector('.box2');
target.appendChild(div);
//cloneNode 克隆元素 传值为true的时候 为深层次克隆 false为浅层次克隆
target2.appendChild(div.cloneNode(true));
} function testInertBefore(){
var box11 = document.querySelector(".box1 > .box11");
var box22 = document.querySelector(".box2 > .box22"); var parent = document.querySelector(".box2"); parent.insertBefore(box11.cloneNode(true),box22);
} function testReplaceChild(){
var box11 = document.querySelector(".box1 > .box11");
var box22 = document.querySelector(".box2 > .box22");
var parent = document.querySelector(".box2"); parent.replaceChild(box11,box22);
}
function testRemoveChild(){
var box22 = document.querySelector(".box2 > .box22");
var parent = document.querySelector(".box2"); parent.removeChild(box22);
} </script>
</head>
<body>
<input type="button" onclick="testCreate()" value="create">
<input type="button" onclick="testInertBefore()" value="testInertBefore">
<input type="button" onclick="testReplaceChild()" value="testReplaceChild">
<input type="button" onclick="testRemoveChild()" value="testRemoveChild">
<hr>
<div class="box1">
<div class="box11">this is a div test 11</div>
<div class="box12">this is a div test 12</div>
<div class="box13">this is a div test 13</div>
<div class="box14">this is a div test 14</div>
</div>
<div class="box2">
<div class="box21">this is a div test 21</div>
<div class="box22">this is a div test 22</div>
<div class="box23">this is a div test 23</div>
<div class="box24">this is a div test 24</div>
</div>
</body>
</html>

js中改变文档的层次结构(创建元素节点,添加结点,插入子节点,取代子节点,删除子节点)的更多相关文章

  1. Linux命令 改变文档权限及所有者

    Linux命令 改变文档权限及所有者 chgrp :改变档案所属群组 chown :改变档案拥有者 chmod :改变档案的权限, SUID, SGID, SBIT等等的特性 chgrp说明及范例 [ ...

  2. ubuntu命令改变文档权限和所有者

    chgrp :改变档案所属群组 chown :改变档案拥有者 chmod :改变档案的权限, SUID, SGID, SBIT等等的特性,可读.可写.可执行 1 chgrp 例子 chgrp [-R] ...

  3. 【2017-03-31】JS-DOM操作:操作属性、彩虹导航栏、定时器、操作内容、创建元素并添加、操作相关元素

    一.操作属性 1.什么是属性: <div class="div" id="div1" style="" ></div> ...

  4. Express 4 更新改变文档

    概览 从 Express 3 到Express 4 是一个巨大的变化,这意味着现存的 Express 3 应用在不更新依赖的情况下将不能工作. 这篇文章涵盖一下内容: Express 4 中的变化 一 ...

  5. js模拟浏览器加载效果 pace.js 中文官方文档

    2017年2月20日12:11:25 官网URL:http://github.hubspot.com/pace/docs/welcome/ 文档 http://github.hubspot.com/p ...

  6. node.js 中的package.json文件怎么创建?

    最近在用webstorm和nodejs做一些东西,老是各种混乱,今天上午创建一个新的项目,结果发现,npm init之后,并没有出现package.json,并没有太明确他的功能的小姑娘表示十分的惊慌 ...

  7. Laya改变文档结构后GameConfig自动生成错误问题

    原来的WeaponPanel,ItemPanel,PetPanel改变了路径,然后GameConfig还是一直生成旧的路径,因为旧路径已经不存在,所以提示报错,编译不过去. 需要把编辑模式下的改路径相 ...

  8. apache 改变文档根目录www的位置

    1.找到apache的安装目录,找到config/httpd.conf,找到DocumentRoot "D:/wamp/www/" 改成你想要的目录,例如:改成 DocumentR ...

  9. js中的对象和数组的创建

    <!DOCTYPE html><html><head> <title>获取控制</title> <meta charset=" ...

随机推荐

  1. C++设计与声明——让接口容易被正确使用

    一个简答易错的例子 class Date { public Date(int month,int day,int year); } 一年后使用这个接口的时候,写了Date d(15,10,2015)或 ...

  2. vuex: 简单(弹窗)实现

    在使用基于 vue.js 2.0 的UI框架 ElementUI 开发网站的时候 , 就遇到了这种问题 : 一个页面有很多表单 , 我试图将表单写成一个单文件组件 , 但是表单 ( 子组件 ) 里的数 ...

  3. 流程设计器jQuery + svg/vml(Demo6 - 增加结点属性及切换)

    到目前流程设计器流程结点的拖拽操作已基本完成,接下来就到结点的属性开发了.前面已经开发过流程模板的属性了,结点属性跟模板属性类似,从属性模板定义copy一份,然后按各结点类型进行调整就ok. 1.先来 ...

  4. python3高阶函数:map(),reduce(),filter()的区别

    转载请注明出处:https://www.cnblogs.com/shapeL/p/9057152.html 1.map():遍历序列,对序列中每个元素进行操作,最终获取新的序列 print(list( ...

  5. C# 序列化详解,xml序列化,json序列化对比

    本文讲讲一些纯技术的东西.并且讲讲一些原理性的东西,和一般的百度的文章不一致,如果你对序列化不清楚,绝对可以很有收获. 技术支持QQ群(主要面向工业软件及HSL组件的):592132877  (组件的 ...

  6. js 验证手机号码

    js 验证手机号码 //验证手机号        function isMobel(value) {           if (/^1[3-8]+\d{9}$/g.test(value)) {    ...

  7. I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard-android.sh hacking

    #!/bin/bash # 参考文章: # . Shell特殊变量:Shell $, $#, $*, $@, $?, $$和命令行参数 # http://c.biancheng.net/cpp/vie ...

  8. window XP下 php5.5+mysql+apache2+phpmyadmin安装

    学了将近大半年的php了,还没有装过独立的php环境,一直用的集成的.记得刚学时,一头雾水,不知改怎么搭配环境,就觉得特别复杂,各种看不懂,今天还是自己在XP环境下搭配了一个. 首先,下载php5.5 ...

  9. Objective-C教程备忘单

    终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...

  10. hasura graphql pg 自定义函数的使用

      hasura graphql 的安装可以参考相关项目 创建函数 数据表创建 CREATE TABLE sql_function_table ( id SERIAL PRIMARY KEY, inp ...