当前位置:网站首页>Acquisition of DOM learning elements JS
Acquisition of DOM learning elements JS
2022-04-23 09:27:00 【Small white egg tart】
Catalog
getElementById according to id To choose
getElementByTagName Select according to the tag name
DOM(Document Object Model) Document object model , yes W3C Organizations recommend ways to handle extensible markup languages (HTML or XML) Standard programming interface . Through these interfaces, you can change the content of the web page 、 Structure and pattern .
DOM Trees
How to get elements
getElementById according to id To choose
var timer = document.getElementById('time');
console.log(timer);
console.log(typeof timer);
// 4. It returns an object
console.dir(timer);
// 5. Print the element object we return , Better view the properties and methods inside .
getElementByTagName Select according to the tag name
// What is returned is the collection of obtained element objects , The resulting object is dynamic , Stored as a pseudo array , With length and index number , But no pop,push Other methods
var lis = document.getElementsByTagName('li');
console.log(lis);
console.log(lis[0]);
console.log(lis.length);
// We want to print the element objects in turn , Take traversal
for (var i = 0; i < lis.length; i++) {
console.log(lis[i]);
}
// There is also a pseudo array in the page
// There is no such element , Returns an empty pseudo array
// obtain ol The inside can't pass document By parent element ( You must specify which element )
// var ol = document.getElementsByTagName('ol');
// The return is still a pseudo array [ol] Form cannot be our parent element
// console.log(ol[0].getElementsByTagName('li'));
// It's a lot of trouble We can give ol To specify a id id Is the only one.
var ol = document.getElementById('ol');
console.log(ol.getElementsByTagName('li'));
getElementByClassName Choose according to the name of the class , The class is xxx All will be selected
// Select according to the name of the class selector
console.log(document.getElementsByClassName('box'));
H5 New ways to get elements
querySelector You can choose according to the selector ( tag chooser , Class selectors ,id Selectors ) Choose the first element
If you want to use all
querySelectorAll
//querySelector(' Selectors '); According to the specified selection, it returns the first element object
// Class selection , label ,id...
// The selectors inside should be marked with their own selectors
console.log(document.querySelector('#nav'));
console.log(typeof document.querySelector('li'));
// 3querySelectorAll(' Selectors ') Returns all... According to the specified selector Pseudo array
console.log(document.querySelectorAll('.box'));
obtain body and html
1. obtain body, direct document.body
2. obtain html,document.documentElement
// 1. obtain body Elements
console.log(document.body);
var bod = document.body;
console.dir(document.getElementsByTagName('div'));
console.dir(bod); //object
//2. obtain html
console.log(document.documentElement);
版权声明
本文为[Small white egg tart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624114184.html
边栏推荐
猜你喜欢
How to protect open source projects from supply chain attacks - Security Design (1)
What is augmented reality technology? Where can it be used?
Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
A must see wechat applet development guide 1 - basic knowledge
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
Open services in the bottom bar of idea
Applet error: cannot read property'currenttarget'of undefined
To remember the composition ~ the pre order traversal of binary tree
MySQL of database -- overview and installation
653. 两数之和 IV - 输入 BST
随机推荐
亚马逊云科技入门资源中心,从0到1轻松上云
Distributed message oriented middleware framework selection - Digital Architecture Design (7)
Go language learning notes - array | go language from scratch
LGB, XGB, cat, k-fold cross validation
How does kubernetes use harbor to pull private images
NLLLoss+log_ SoftMax=CE_ Loss
npm ERR! network
Cloud computing competition -- basic part of 2020 competition [task 3]
112. Path sum
NPM installation yarn
How to protect open source projects from supply chain attacks - Security Design (1)
Arbre de dépendance de l'emballage des ressources
错题汇总1
MySQL - Chapter 1 (data type 2)
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
JSON input of Chapter 14 of kettle paoding jieniu
Image processing in opencv -- Introduction to contour + contour features
Vivo, hardware safe love and thunder
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
SAP 101K 411K 库存变化