当前位置:网站首页>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
边栏推荐
- 条形码识别性能低,如何优化Dynamsoft Barcode Reader解码性能
- Test the necessary skill points of siege lion! This article takes you to interpret the testing technology under Devops
- 聊聊先享後付
- ImageMagick - 添加水印
- 测试攻城狮必备技能点!一文带你解读DevOps下的测试技术
- Opencv computer vision learning (10) -- image transform (Fourier transform, high pass filter, low pass filter)
- PHP后门隐藏技巧
- Mate 40 series launch with Huawei sports health service to bring healthy digital life
- 滴滴的分布式ID生成器(Tinyid),好用的一批
- How does varhart xgantt represent working days on a calendar
猜你喜欢

11. Service update

HMS Core推送服务,助力电商App开展精细化运营

深入浅出大前端框架Angular6实战教程(Angular6、node.js、keystonejs、

The first choice for lightweight GPU applications is the NVIDIA vgpu instance launched by Jingdong Zhilian cloud

K-vim installation and the ycmd server shut down (restart with ': ycmrestartserver')

Win7 AppCrash (solution)

Detailed software engineering -- the necessary graphs in each stage

Dynamsoft barcode reader v7.5!

How to write plug-in code of small program mall system? How to use code to check whether the plug-in is successfully added?

Plug in bilibilibili new version 0.5.5
随机推荐
VARCHART XGantt入门教程
廬山真面目之二微服務架構NGINX版本實現
Microservice - how to limit and fuse service current
RFID fixed assets management system for fire equipment
HMS core push service helps e-commerce app to carry out refined operation
win7如何快速打cmd并到达所要的目录
Three steps to understand Kerberos Protocol easily
测试攻城狮必备技能点!一文带你解读DevOps下的测试技术
【原創】ARM平臺記憶體和cache對xenomai實時性的影響
C enumerates the differences between permissions |, and |
The advantages and functions of psychological counseling app
Bluetooth broadcast chip for Shanghai giant micro
Mate 40系列发布 搭载华为运动健康服务带来健康数字生活
Design pattern of facade and mediator
抽絲剝繭——門面和調停者設計模式
Talk about sharing before paying
Web API interface design: swaggerui document / unified response format / unified exception handling / unified authority verification
11.Service更新
垃圾分类知识竞赛
Rech8.0 learning days 12 rh134