当前位置:网站首页>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
边栏推荐
- Reference frame generation based on deep learning
- 拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
- c#使用oracle存储过程获取结果集实例
- 性能测试过程和计划
- Nodejs tutorial expressjs article quick start
- 967- letter combination of telephone number
- KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
- js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序
- Regular expression collection
- 强化学习-学习笔记5 | AlphaGo
猜你喜欢
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
OneNote 深度评测:使用资源、插件、模版
None of the strongest kings in the monitoring industry!
[MySQL] trigger
OneNote in-depth evaluation: using resources, plug-ins, templates
Redis insert data garbled solution
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
Manifest of SAP ui5 framework json
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
随机推荐
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
Aiko ai Frontier promotion (7.6)
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
【mysql】触发器
966 minimum path sum
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
OSPF多区域配置
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
嵌入式开发的7大原罪
面试官:Redis中有序集合的内部实现方式是什么?
PG基础篇--逻辑结构管理(事务)
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Regular expression collection
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
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
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
Taylor series fast Fourier transform (FFT)