当前位置:网站首页>JS operation DOM element (I) -- six ways to obtain DOM nodes
JS operation DOM element (I) -- six ways to obtain DOM nodes
2022-07-06 21:10:00 【viceen】
JS operation dom Elements ( One )—— obtain DOM Six ways of nodes
js obtain Dom Common methods of nodes : Element nodes , Attribute node , Text node .
- adopt ID obtain (getElementById)
- adopt name attribute (getElementsByName)
- By tag name (getElementsByTagName)
- By class name (getElementsByClassName)
- Get an element through a selector (querySelector)
- Get a set of elements through a selector (querySelectorAll)
- obtain html Methods (document.documentElement)
document.documentElement It's about getting html This label's - obtain body Methods (document.body) document.body It's about getting body This label's .
1.getElementById() — id Selected elements
var getid=document.getElementById('id name ')
2.getElementsByClassName() — Class name selected element
var getclass=document.getElementsByClassName(' Class name ')
3.getElementsByTagName() — Tag name selected element
var gettag=document.getElementsByTagName(' Tag name ')
4.getElementsByName() — adopt name Attribute get element
var getname=document.getElementsByName('name name ')
Next, get the writing method and css The selector of is written in the same way
5.querySelector() — Get an element accurately
var f=document.querySelector("#tabelList li:nth-child(2)")
6.querySelectorAll() Get all elements that meet the conditions such as class name or tag name
var g=document.querySelectorAll('.box p>span')
1、getElementById() — adopt id Selected elements
The context must be document.
Parameters must be passed , Parameter is string type , Is to get the element id.
The return value only gets one element , No return found null.
function func() {
var selectId = document.getElementById("id name ");
selectId.style.color = "blue";
}
func();
2、getElementsByName — adopt name Attribute get element
Generally used in form elements
The context must be document. Content
Parameters must be passed , The parameter is to get the element name attribute .
The return value is an array of classes , Return empty array not found .
function funa() {
var selectDiv = document.getElementsByName("status");
selectDiv[0].checked = true;
// here selectDiv yes NodeList Node object
// console.log(selectDiv)
}
funa()
document.getElementsByName('name')
3、getElementsByTagName() — Select the element by the tag name
The context can be document, It can also be an element , Note that this element must exist .
The parameter is to get the tag name attribute of the element , Case insensitive .
The return value is an array of classes , Return empty array not found
function func() {
var selectTag = document.getElementsByTagName(" Tag name ");
selectTag.style.color = "blue";
}
func();
var obj = document.getElementsByTagName('div');
for(let i = 0; i<obj.length; i++){
obj[i].onclick = function(e){
console.log(i)
}
}
4、getElementsByClassName() — Select the element by class name
The context can be document, It can also be an element .
The parameter is the class name of the element .
The return value is an array of classes , Return empty array not found .
function func() {
var selectClass = document.getElementsByClassName(" Class name ");
selectClass.style.color = "blue";
}
func();
var obj1 = document.getElementsByClassName('animated')
// console.log
0:div.app.animated
1:div#login.login.animated.rubberBand
2:div#reg.reg.animated.shake
3:div#kefu.kefu.animated.swing
4:div#LoginState.state.animated.bounce
5:div.loginState.animated
6:div.regState.animated
7:div.pop.animated
4、querySelector and queryselectorAll
Get the notation and... In the parentheses of the element css Selector Write the same way
5-1 querySelector() — Get an element accurately
The context can be document, It can also be an element .
Parameters are selectors , Such as :”div .className”.
The return value only gets the first element .
function funb() {
var selectDiv = document.querySelector("#list li:nth-child(3)");
selectDiv.style.color = "red";
// console.log(selectDiv)
}
funb()
document.querySelector('.animated')
5-2 queryselectorAll() — Get a set of elements through a selector
Get all elements that meet the conditions such as class name or tag name
The context can be document, It can also be an element .
Parameters are selectors , Such as :”div .className”.
The return value is an array of classes .
function func() {
var selectDiv = document.querySelectorAll(".text ul>li");
// here selectDiv Is an array , Stored li
selectDiv[0].style.color = "red";
// Print out as NodeList object -- It's a collection of nodes , have access to Array.from() Convert it to an array
console.log(selectDiv)
}
func()
document.querySelector('.animated')
6、document.title and document.body
function fund() {
document.title = " This is a title Elements ";
document.body.innerHTML = "<p style='color: red'> This is a body Elements </p>";
}
notes :getElementByTagName Can operate dynamically created Dom, and getElementById Can not be
边栏推荐
- Pat 1085 perfect sequence (25 points) perfect sequence
- 2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- 1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
- ##无yum源安装spug监控
- Performance test process and plan
- After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
- [sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
- 3D人脸重建:从基础知识到识别/重建方法!
- [200 opencv routines] 220 Mosaic the image
猜你喜欢
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Is it profitable to host an Olympic Games?
PHP saves session data to MySQL database
强化学习-学习笔记5 | AlphaGo
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
面试官:Redis中有序集合的内部实现方式是什么?
Swagger UI教程 API 文档神器
966 minimum path sum
1_ Introduction to go language
随机推荐
Huawei device command
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
Deployment of external server area and dual machine hot standby of firewall Foundation
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
PG基础篇--逻辑结构管理(事务)
js通过数组内容来获取数组下标
2017 8th Blue Bridge Cup group a provincial tournament
强化学习-学习笔记5 | AlphaGo
愛可可AI前沿推介(7.6)
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
1_ Introduction to go language
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
How to implement common frameworks
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
Common English vocabulary that every programmer must master (recommended Collection)
数据湖(八):Iceberg数据存储格式
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Swagger UI tutorial API document artifact
SAP UI5 框架的 manifest.json