当前位置:网站首页>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
边栏推荐
- KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
- 2017 8th Blue Bridge Cup group a provincial tournament
- Nodejs tutorial expressjs article quick start
- What's the best way to get TFS to output each project to its own directory?
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- 038. (2.7) less anxiety
- Variable star --- article module (1)
- ##无yum源安装spug监控
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- js通过数组内容来获取数组下标
猜你喜欢
OAI 5G NR+USRP B210安装搭建
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
3D人脸重建:从基础知识到识别/重建方法!
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
强化学习-学习笔记5 | AlphaGo
【mysql】触发器
面试官:Redis中有序集合的内部实现方式是什么?
Pycharm remote execution
The biggest pain point of traffic management - the resource utilization rate cannot go up
随机推荐
Spark SQL chasing Wife Series (initial understanding)
How to turn a multi digit number into a digital list
Reflection operation exercise
【mysql】触发器
#yyds干货盘点#重新梳理箭头函数的this
愛可可AI前沿推介(7.6)
Data Lake (VIII): Iceberg data storage format
It's almost the new year, and my heart is lazy
Xcode6 error: "no matching provisioning profiles found for application"
js中,字符串和数组互转(二)——数组转为字符串的方法
Nodejs tutorial expressjs article quick start
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
3D人脸重建:从基础知识到识别/重建方法!
面试官:Redis中有序集合的内部实现方式是什么?
代理和反向代理
What key progress has been made in deep learning in 2021?
What is the difference between procedural SQL and C language in defining variables
Regular expression collection
Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
OneNote 深度评测:使用资源、插件、模版