1.概念
文档对象模型(document object model )
HTML DOM 将 HTML 文档视作树结构。这种结构被称为节点树:DOM Tree
2.DOM加载顺序(html从代码–>真正显示)
- 解析HTML结构(从上向下的过程)
- 加载外部脚本和样式表文件
- 解析并执行脚本代码
- 构造HTML DOM模型
- 加载图片等外部文件
- 页面加载完毕
3.节点
dom树中的节点彼此拥有层级关系(由节点构成)
- 整个文档是一个文档节点
- 每个 HTML 元素是元素节点
- HTML 元素内的文本是文本节点
- 每个 HTML 属性是属性节点
- 注释是注释节点
- 在节点树中,顶端节点被称为根(root)
- 每个节点都有父节点、除了根(它没有父节点)
- 一个节点可拥有任意数量的子节点
- 父、子 和同胞(兄弟或姐妹)节点。
- 同胞是拥有相同父节点的节点
4.document方法和属性
获得节点的常用方法
- getElementById():通过id属性获得节点
- 例1:
例2:点解图片,换位另外一张
点击前:
点击后:
-
getElementsByName():通过name属性获得节点
-
getElementsByTagName():通过标签名获得节点
5.Element 对象
- Element 对象表示 HTML 元素(节点)对象
- Element 对象是拥有类型为元素节点、文本节点、注释节点等子节点的节点(标签)
- 部分方法
element.appendChild()向元素添加新的子节点,作为最后一个子节点
例0:(简洁版)
<ul id="myList"><li>Coffee</li><li>Tea</li></ul>
<input id="input">
<button onclick="myFunction()">添加</button>
<script>
function myFunction(){
var node=document.createElement("LI");
var input=document.getElementById("input")
var textnode=document.createTextNode(input.value);
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
}
</script>
结果:
例1:(注释详细)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul id="ul1">
<li>大娃</li>
<li>二娃</li>
<li>三娃</li>
<li>四娃</li>
<li>五娃</li>
</ul>
<input id="inputstr"><button onclick="add()">添加</button>
<script>
function add() {
//获得input这个节点
var inputNode=document.getElementById('inputstr')
//用户的输入值
var inputValue=inputNode.value
//根据用户输入的值,创建一个文本节点
var textNode=document.createTextNode(inputValue)
//创建一个节点(创建一个li节点)
var liNode=document.createElement("li");//li就是要创建目标节点的标签名
//给li节点添加一个子文本节点
liNode.appendChild(textNode)
//获得input这个节点(待改造的对象)
var ulNode=document.getElementById("ul1")
//给ul这个节点添加一个新的孩子,newChild
ulNode.appendChild(liNode)
//给input这个节点的value置空
inputNode.value=""
}
</script>
</body>
</html>
结果阶段a:
结果阶段b:
例2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="z">
待添加:
</button>
<input id="inputstr"><button onclick="add()">添加</button>
<script>
function add() {
//获得input这个节点
var inputNode=document.getElementById('inputstr')
//用户的输入值
var inputValue=inputNode.value
//根据用户输入的值,创建一个文本节点
var textNode=document.createTextNode(inputValue)
//创建一个节点(创建一个button节点)
var newbuttonNode=document.createElement("button")
//给button节点添加一个子文本节点
newbuttonNode.appendChild(textNode)
//获得input这个节点
var buttonNode=document.getElementById("z")
//给ul这个节点添加一个新的孩子,newChild
buttonNode.appendChild(newbuttonNode)
//给input这个节点的value置空
inputNode.value=""
}
</script>
</body>
</html>
结果阶段a:
结果阶段b:
element.removeChild():从元素中移除子节点(输入指定位置删除)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul id="myList">
<li>Coffee</li>
<li>Tea</li><li>Milk</li>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
节点位置:<input id="input">
<button onclick="myFunction()">删除</button>
<script>
function myFunction(){
//获得id名为input的input节点
var input=document.getElementById("input")
//获得id名为myList的List节点
var list=document.getElementById("myList");
//list.childNode获得list的所有节点,获得输入对象input的位置值input.value
list.removeChild(list.childNodes[input.value]);
}
</script>
</body>
</html>
结果阶段a:
结果阶段b:
element.replaceChild():替换元素中的子节点
Eg1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul id="ul1" >
<li>zs</li>
<li>ls</li>
<li>wu</li>
</ul>
改变位置:<input id="inputtag"><br>
改变内容:<input id="inputstr">
<button onclick="changeli()">改变</button>
<script>
function changeli() {
// 获得下标, 以及改变的内容
var inputTagNode = document.getElementById('inputtag')
var inputStrNode = document.getElementById('inputstr')
var liNode = document.createElement('li')
var textNode = document.createTextNode(inputStrNode.value)
liNode.appendChild(textNode)
var ulNode = document.getElementById('ul1')
// 参数要求: 新节点, 旧结点
ulNode.replaceChild(liNode , ulNode.childNodes[inputTagNode.value])
}
</script>
</body>
</html>
结果:
Eg2:(交换图片)
var inputN=document.getElementById("imgchange")
var img=document.createElement("img");
img.src=“----";
inputN.replaceChild(img,inputN.childNodes[1]);
Element 对象表示 HTML 元素(节点)
Element 对象是拥有类型为元素节点、文本节点、注释节点的子节点
部分方法:
innerText&innerHTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function changeLink(){
document.getElementById('aaa').innerHTML="<H1>baidu</H1>";
document.getElementById('bbb').innerText="<H1>baidu</H1>";
document.getElementById('aaa').href="http://www.baidu.com";
document.getElementById('bbb').href="http://www.baidu.com";
document.getElementById('aaa').target="_blank";
document.getElementById('bbb').target="_blank";
}
</script>
</head>
<body>
<a id="aaa" href="http://www.microsoft.com">123</a>
<a id="bbb" href="http://www.microsoft.com">456</a>
<input type="button" onclick="changeLink()" value="更改链接">
</body>
</html>
结果阶段a:
结果阶段b: