当前位置:网站首页>DOM节点操作
DOM节点操作
2020-11-07 17:19:00 【程序猿欧文】
DOM节点操作
1. 节点概述
节点的三个基本属性:
- 节点类型nodeType
- 节点名称nodeName
- 节点值nodeValue
- 元素节点 nodeType为1(实际开发中的主要操作)
- 属性节点 nodeType为2
- 文本节点 nodeType为3(文本节点包含文字、空格、换行等)
2. 节点层级
利用DOM树可以把节点划分为不同的层级关系,常见的是父子兄层级关系。
2.1 父级节点
node.parentNode
- parentNode属性可以返回某节点的最近一个父节点
- 如果指定的节点没有父节点,则返回nill
2.2 子节点
1.parentNode.childNodes // 标准
parentNode.childNode返回包含指定节点的子节点的集合,该集合为即时更新的集合。
注:
- 返回值里包含了所有的节点,包括元素节点、文本节点等
- 如果只想获得里面的元素节点,则需要专门处理。所以这种情况下不建议使用childNodes
// 只想获得里面的元素节点的处理var eg = document.querySelector('ol');for(var i = 0;i < ol.childNodes.length;i++) { if (ol.childNodes[i].nodeType == 1) { // 这样就可以选出元素节点 ...... }}
2.parentNode.children // 非标准
parentNode.children是一个只读属性,返回所有的子元素节点。它只返回子元素节点。虽然它是非标准,但是得到了各个浏览器的支持,因而可以放心使用。
3.parentNode.firstChild
parentNode.firstChild返回第一个子节点,找不到返回null。返回值里包含了所有的节点,包括元素节点、文本节点等。
4.parentNode.lastChild
parentNode.lastChild返回最后一个节点,找不到返回null。返回值里包含了所有的节点,包括元素节点、文本节点等。
5.parentNode.firstElementChild
parentNode.firstElementChild返回第一个子元素节点,找不到返回null。
6.parentNode.lastElementChild
parentNode.lastElementChild返回最后一个子元素节点,找不到返回null。
注:5、6方法存在兼容性问题,ie9以上才支持。
由于firstChild和lastChild包含其他节点,而firstElementChild和lastElementChild又有兼容性问题,所以获取第一个子元素节点或最后一个子元素节点的解决方法为:
- 如果想要第一个子元素节点,可以使用parentNode.children[0]
- 如果想要最后一个子元素节点,可以使用parentNode.children[parentNode.children.length-1]
2.3 兄弟节点
1.node.nextSibling
nextSibling返回当前元素的下一个兄弟节点,找不到返回null。返回值里包含了所有的节点,包括元素节点、文本节点等。
2.node.previousSibling
previousSibling返回当前元素上一个兄弟节点,找不到返回null。返.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4707624
边栏推荐
- 微服務 - 如何進行服務限流和熔斷
- Plug in bilibilibili new version 0.5.5
- Come on in! Take a few minutes to see how reentrantreadwritelock works!
- A good thing for working people -- to temper the will of iron and steel requires such an efficient computer
- Mobile pixel adaptation scheme
- JS string - string string object method
- 条形码识别器Dynamsoft Barcode Reader v7.5全新上线!
- K-vim installation and the ycmd server shut down (restart with ': ycmrestartserver')
- gitlab 持续集成开发环境之CI/CD
- Dynamsoft barcode reader v7.5!
猜你喜欢
Exclusive interview with Yue Caibo
Three steps to understand Kerberos Protocol easily
Exclusive interview with alicloud database of | 2020 PostgreSQL Asia Conference: Wang Xu
课堂练习
RFID fixed assets management system for fire equipment
Jenkins pipline stage setting timeout
三步轻松理解Kerberos协议
Come on in! Take a few minutes to see how reentrantreadwritelock works!
南京标识标牌设计制作,导视VI系统设计
Insomnia all night
随机推荐
一种超参数优化技术-Hyperopt
C# 枚举权限 |和||,&和&&的区别
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Developing STM32 USB with cubemx
微服務 - 如何進行服務限流和熔斷
Insomnia all night
2020-11-06: go, let's talk about the scheduler.
抽絲剝繭——門面和調停者設計模式
Yum [errno 256] no more mirrors to try solution
Come on in! Take a few minutes to see how reentrantreadwritelock works!
如何解决谷歌Chrome浏览器空白页的问题
7.Swarm搭建集群
Chinese sub forum of | 2020 PostgreSQL Asia Conference: Pan Juan
A good thing for working people -- to temper the will of iron and steel requires such an efficient computer
使用LWA和Lync模拟外部测试无边缘单前端环境
VARCHART XGantt如何在日历上表示工作日
Characteristics of magnetic memory chip STT-MRAM
yum [Errno 256] No more mirrors to try 解决方法
大佬们如何在nginx镜像里面增加模块?
[doodling the footprints of Internet of things] Introduction to Internet of things