dom 节点对象
Dom节点认识
从dom角度,整个html文档就是一个对象,每个html标签、标签属性、内容、注释…都被看成dom节点
DOM 就是我们 html 结构中一个一个的节点构成的
-
Dom 节点分类
- 节点类型 1.整个文档是一个文档节点 2.每个 HTML 元素是元素节点 3.HTML 元素内的文本是文本节点 4.每个 HTML 属性是属性节点 5.注释是注释节点 - 节点对象分类: 元素节点 getElementBy 获取 文本节点 innerText 获取 属性节点 getAttribute 获取 doucment文档节点 注释节点
**html文档结构:**树型结构
**节点关系:**父子关系 兄弟关系
获取Dom节点
- getElement系列
getElementById() - querySelector系列
ele.innerHTML - 层次结构
children :获取某一节点下所有的子一级 元素节点
层次关系获取元素节点
firstElementChild:
lastElementChild
获取元素节点的所有属性节点: attributes
层次图
非 常规节点获取
- 获取html根节点 document.documentElement
- 获取body节点 document.body
- 获取head document.head
案例——购物车
//练习 1.通过层次结构获取dom节点
//2. 熟悉循环遍历数组,绑定数组项点击事件
//3. 掌握this关键字用法
//4. 熟悉字符串截取类型转换取小数位置等常规操作
节点属性
用于判断节点的类型
节点类型 节点名称 节点内容
操作DOM节点
对节点进行增删改查。
-
创建节点
createAttribute: 创建属性节点 setAttruibuteNode: 给元素节点设置属性节点
-
加入节点
1、appendChild:是向一个元素节点的末尾追加一个节点
2、insertBefore:向某一个节点前插入一个节点
-
删除节点
removeChild:移除某一节点下的某一个节点
remove: 移除当前节点
语法:oDiv.remove() 移除oDiv节点
-
替换节点
replaceChild:将页面中的某一个节点替换掉
-
复制节点
语法: 节点.cloneNode(true|false)
true: 复制节点包含节点下所有子节点
false: 复制当前节点 默认方式
获取元素的非行间样式
了解内容:
getComputedStyle(非IE使用)
currentStyle(IE使用)
获取元素的偏移量
页面与窗口的距离 我们有几个属性来获取,offsetLeft 和 offsetTop 和 offsetWidth 和 offsetHeight
offsetLeft 和 offsetTop:
offsetWidth 和 offsetHeight:
获取元素尺寸(宽、高)三种方式
练习:
全选框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全选框</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<table class="m-table">
<caption>员工信息列表</caption>
<tbody>
<tr>
<th><input type="checkbox" class="checkbox-all" onclick="checkboxAll()">全选</th>
<th>员工编号</th>
<th>员工名称</th>
<th>员工部门</th>
<th colspan="2">操作</th>
</tr>
<tr>
<td> <input type="checkbox" class="checkbox-item" onclick="checkBoxSingle()"></td>
<td>1001</td>
<td>张三</td>
<td>技术部</td>
<td><a href="#">删除</a> </td>
<td><a href="#">编辑</a></td>
</tr>
<tr>
<td> <input type="checkbox" class="checkbox-item" onclick="checkBoxSingle()"></td>
<td>1002</td>
<td>李四</td>
<td>技术部</td>
<td><a href="#">删除</a> </td>
<td><a href="#">编辑</a></td>
</tr>
<tr>
<td> <input type="checkbox" class="checkbox-item" onclick="checkBoxSingle()"></td>
<td>1003</td>
<td>王二</td>
<td>技术部</td>
<td><a href="#">删除</a> </td>
<td><a href="#">编辑</a></td>
</tr>
</tbody>
</table>
</div>
<script src="js/my.js"></script>
</body>
</html>
*{padding: 0; margin: 0;}
ul li{list-style: none;}
a{text-decoration: none; color: #706e6e;}
.container{width: 80%; margin: 5px auto;}
.m-table,th,td{border: 1px solid #cecece; border-collapse: collapse; }
.m-table caption{font-size: 18px; font-weight: bold;}
.m-table tr{line-height: 40px;}
.m-table tr:hover{background-color: #eeeaea;}
.m-table th{width: 200px; background-color: goldenrod; color: white; text-align: center; }
.m-table td{text-align: center; }
/**
* 全选框
*
*/
function checkboxAll() {
let checkBoxAllEle = document.querySelector('.checkbox-all');
let checkBoxArrayEle = document.querySelectorAll('.checkbox-item');
for (let index = 0; index < checkBoxArrayEle.length; index++) {
const element = checkBoxArrayEle[index];
if (checkBoxAllEle.checked == true) {
element.checked = true;
} else {
element.checked = false;
}
}
}
/**
* 分析:
* 只有一个复选框未选中,全选框设为未选中;复选框全被选中全选框设为选中。
*/
function checkBoxSingle() {
let checkBoxAllEle = document.querySelector('.checkbox-all');
let checkBoxArrayEle = document.querySelectorAll('.checkbox-item');
let isCheck = true; // 全选框是否选中
for (let index = 0; index < checkBoxArrayEle.length; index++) {
const element = checkBoxArrayEle[index];
if(element.checked == false){
isCheck = false;
break;
}
}
if(isCheck){
checkBoxAllEle.checked = true;
}else{
checkBoxAllEle.checked = false;
}
}
注意
document.getElementByClassName会更新旧的查询结果
document.querySelector则不会
比如: document.querySelectorAll(‘.li1’)
=> 获取所有类名为li1的元素,长度为3
=> 动态添加一个类名li1元素,长度还是为3