js基础总结01 --操作DOM
2024-09-01 17:54:29
1、选择对象
- 通过id来选择绑定一个dom节点 :document.getElementById('p1');
- 通过类名来绑定一个类数组的对象集合,:document.getElementsByClassName('p');//类似的还有 document.getElementsByName、document.getElementsByName等
- 通过css选择器来返回第一个匹配的dom节点:document.querySelector('#p1');
- 通过css选择器来返回一个类数组的对象集合:document.querySelectorAll('p');
- 应尽量避免用queryselector和queryselectorAll来查询绑定对象,因为这样效率慢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<p id="p1">1</p>
<p class="p">2</p>
<p class="p">3</p>
<script>
window.onload = function(){
document.getElementsByName
document.getElementsByTagName
console.log(document.getElementById('p1'));//<p id="p1">1</p>
console.log(document.getElementById('p2'));//null,找不到
console.log(document.getElementsByClassName('p'))//HTMLCollection(2) [p.p, p.p],返回类数组的对象集合
console.log(document.querySelector('#p1'));//<p id="p1">1</p>
console.log(document.querySelector('#p2'));//null,找不到返回null
console.log(document.querySelector('p'));//<p id="p1">1</p>,返回匹配到的第一个节点
console.log(document.querySelectorAll('p'));//NodeList(3) [p#p1, p.p2, p],返回匹配到的类数组的对象集合
console.log(document.querySelectorAll('p').pop());//test.html:24 Uncaught TypeError: document.querySelectorAll(...).pop is not a function
}
</script>
</body>
</html>
示例
注意:document.getElementsByClassName('p')等返回的是一个HTMLCollection 对象集合,document.querySelectorAll('p')返回的是一个NodeList 对象集合,两者没有很大的不同,但要注意‘访问 HTMLCollection 项目,可以通过它们的名称、id 或索引号访问 NodeList 项目,只能通过它们的索引号’--这是w3School里的解释。
注意:类数组的对象集合之所以不能称为数组,是因为它们没有数组的pop(),join()等方法
2、操作属性
- 直接通过修改style来修改属性:document.getElementById('p').style.color = 'red'; .cssText += 'color:red;display:block;'//集合操作
- 通过添加类名来实现:document.getElementById('p').className = 'red';//会覆盖原有的类 ~~~改为document.className += ' red',就不会了
- 通过节点的classList添加一个类:document.getElementById('p').classList.add('red');//不会覆盖原有的,从尾部添加
- 通过setAttribute来直接向html标签中添加class="xxx"属性:document.getElementById('p').setAttribute('class','red');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
<style>
.red{
color:red;
}
</style>
</head>
<body>
<p id="p" class="p-default">1</p>
<p>2</p>
<script>
window.onload = function(){
// document.getElementById('p').style.color = 'red';
// document.getElementById('p').className = 'red';//直接给选定节点设置类名,会覆盖掉原有的类
// document.getElementById('p').classList.add('red');//直接给节点添加一个类,排在原有类的后面,添加已有的类名。及不操作
// document.getElementById('p').classList.remove('p-default');//删去选定的类
// document.getElementById('p').setAttribute('class','red');//直接向html标签里添加class="xxx"的属性
}
</script>
</body>
</html>
示例
3、绑定事件
- 直接才html标签中绑定事件,通过<p onclick="functionName()"></p>,类似的形式进行绑定
- 对绑定好的dom对象, document.getElementById('p').onclick = function(){}类似的方法进行绑定
- 添加事件监听的方式 document.getElementById('p').addEventListener('click',function(){})进行绑定
4、获取属性和值
- 获取html内容:document.getElementById('p').innerHTML
- 直接通过属性名获取:document.getElementById('p').属性名
- 通过getAttribute获取:document.getElementById('p').getAttribute('属性名')
5、操作节点
6、节点类型(2019-11-27 补充)
一共12种,以下为常用的,每个类型的节点都有相应的nodeName和nodeValue和nodeType等
代码实例:
<body>
<div id="box" class="div">
<p>123</p>
<!-- <span>111</span> --></div>
<script>
var oBox = document.getElementById('box');
console.log(oBox.ELEMENT_NODE);//1,元素节点
var oBoxAttrNode = oBox.attributes;//属性节点集合
console.log(oBoxAttrNode[0].nodeType);//2,属性节点
console.log(oBoxAttrNode[0].ATTRIBUTE_NODE);//2,属性节点
console.log(oBoxAttrNode[0].nodeName+' '+oBoxAttrNode[0].nodeValue);//属性节点的名和值
var oBoxTextNode = oBox.getElementsByTagName('p')[0].firstChild;
console.log(oBoxTextNode.nodeType);//3,文本节点
console.log(oBoxTextNode.nodeName+' '+oBoxTextNode.nodeValue);//#text 123
var oBoxCommentNode = oBox.lastChild;
console.log(oBoxCommentNode.COMMENT_NODE);//8注释节点
console.log(oBoxCommentNode.nodeType+' '+oBoxCommentNode.nodeName+' '+oBoxCommentNode.nodeValue); //8, 不能有空格 </script>
</body>
7、浏览器渲染Dom节点的顺序
- 处理html并构建dom树
- 处理css等文件并构建cssom树
- 将dom树和cssom树合并成一个渲染树(节点不一定同dom树,有display:none等情况)
- 根据渲染树来计算每个节点的位置和尺寸
- 将每个节点绘制到屏幕上
注:在解析dom树时遇到script标签或外连的js文件,及js里修改变动dom节点都会导致上述渲染过程重复执行,js操作dom耗时长有以上原因,
解析js中,dom节点没有解析时会出现错误等,这时script标签的属性defer(延迟执行)和async(异步执行)就可以用来控制js中影响dom操作的加载顺序
8、节点的嵌套顺序
常见html的元素节点分为块级元素和行内元素,顾名思义,块级元素是一个块可以设置框高,且独占一行(width:默认为100%),行内元素大部分不能设置宽高,长度随内容撑开。
注:以上为默认的css属性,显示修改的话会按照设置的属性运行,如:display:inline-block,可以将一个行内元素变成可设置宽高
常见的块级标签:div、h1~h6、p、form、ul、ol、li、table、tr、td、dd、dt等
常见的行内标签:span、label、img、a、import、select、input、textarea、button等,其中img,textarea为特殊的可设置宽高的行内元素
节点的嵌套规则:行内元素不能包含块级元素,块级元素可以包含行内元素和块级元素
注:h1、h2、h3、h4、h5、h6、p、dt等只能包含行内元素
9、添加or删除节点
创建一个新节点:
var node = document.createElement('P');
添加新节点到指定的节点中:append() or appendChild()
oDiv.appendChild(node);
添加新节点到指定的节点的位置中:insertBefore(newNode,oldNode) //将newNode插入到oldNode的前面
oDiv.insertBefore(node, oldNode);
删除指定节点的子节点:
ol.removeChild(ol.children[2]);
以上要注意是什么时候是 HTMLCollection,什么时候是NodeList,两者的具体区别,待后续补充吧。
最新文章
- Dnsmasq安装与配置
- Sublime Text 3 简体中文汉化包
- 【Asp.Net】document.getElementById 的属性介绍
- OVER(PARTITION BY)函数介绍
- ————————————————————————————杭电ACM————————————————X-POWER————————————————————————————————
- GET与POST提交
- HDOJ-ACM1003(JAVA)
- js:通过正则匹配获取页面的url中的参数
- crontab中使用python无法执行
- Vux配置指南
- respondsToSelector
- python批量修改文件内容及文件编码方式的处理
- 让ASP.NET Core支持GraphQL之-GraphQL的实现原理
- 不定参数对arguments对象的影响
- docker部署nginx
- linux操作系统中oracle数据库的密码过期问题解决
- Centos 7 Docker安装配置
- Charles做代理的Map Remote路径配置
- js url?callback=xxx xxx的介绍
- Modify Dokuwiki Email Template 修改 Dokuwiki 邮件模板
热门文章
- 设置Activity全屏的方法:
- Spring Boot 2.x基础教程:Swagger静态文档的生成
- maven更新慢,改用国内镜像地址
- 渗透-简单制作过waf的中国菜刀
- PHP get_class_vars 和 (array)
- [CODEVS1537] 血色先锋队 - BFS
- 利用WinRM实现内网无文件攻击反弹shell
- opencv::Brisk检测与匹配
- opencv::调整图像亮度与对比度
- HDU 6607 Time To Get Up(状态压缩+枚举)