当前位置:网站首页>DOM introduction and query
DOM introduction and query
2022-07-22 19:29:00 【Please call me Gerald 6】
When the web page is loaded , The browser will create the document object model of the page (Document Object Model)
HTML DOM The model is constructed as a tree of objects
Through the programmable object model ,JavaScript Gain enough ability to create dynamic HTML
- JavaScript Can change all of the pages HTML Elements
- JavaScript Can change all of the pages HTML attribute
- JavaScript Can change all of the pages CSS style
- JavaScript Be able to react to all events on the page
lookup HTML Elements
Usually , adopt JavaScript, You need to operate HTML Elements
To do this , You must first find the element . There are three ways to do it :
adopt id find HTML Elements
Found by tag name HTML Elements
Found by class name HTML Elements
node.getElementById(“id”)__ adopt id lookup HTML Elements
example :
node.getElementsByTagName(“TagName”)__ Find a group by tag name HTML Elements
Be careful , This method returns us an array of class objects , All the found elements will be encapsulated in the object
example :
node.getElementsByName(“Name”)__ adopt name Property to find a set HTML Elements
Be careful , This method also returns an array of classes
Get the child node of the element node
Call through a specific element node
1.getElementsByName()
- Method , Returns the specified label name of the current node and the descendant node
2.childNodes
- attribute , Represents all the children of the current node
But notice :childNodes Property gets all nodes, including text nodes , according to DOM label The blank space between tags will also be used as text nodes (IE8 And the following browsers , Do not treat blank text as child nodes , So this attribute is in IE8 Will return 4 Child elements and other browsers are 9 individual )
Add a little more , have access to children attribute , You can get all the child elements of the current element
3.firstChild
- attribute , Represents the first child of the current node ( Include blank text nodes )
eject undefined Because what we get is : Parent node ul Heel child node li Blank text nodes between !
If you don't want to delete the blank text node , have access to firstElementChild( Are not compatible IE8 And the following browsers ) To get the first child node of the current element
4.lastChild
- attribute , Represents the last child of the current node ( Follow firstChild equally , It also includes blank text nodes )
Get the parent node and the sibling node
Call... Through specific nodes
1.parentNode
- attribute , Represents the parent of the current node
2.previousSibling
- attribute , Represents the previous sibling of the current node
3.nextSibling
- attribute , Represents the next sibling of the current node
DOM Other methods of query
1. stay document There is a property in body, What it keeps is body References to
var body = document.body;
2.document.documentElement What is kept is html Root tag
var html = document.documentElement;
3.document.all Represents all elements in the page
var all = document.all;
4_①. According to the class Attribute value to query a set of element node objects
getElementsByClassName() According to class Property value to get a set of element node objects , I won't support it IE8 And the following browsers
var box = document.getElementsByClassName("box");
4_②. although IE8 There is no getElementsByClassName() But you can use querySelector() Instead of
document.queryselector()
You need a string of selectors as arguments , According to one css Selector to query an element node object
The disadvantage is that the method always returns a unique element , If there are multiple elements that meet the condition , Then it will only return to the first !
var div = document.querySeletor(".box1 div");
var box1 = document.querySeletor(".box1");
4_③document.querySeletorAll()
The method and querySeletor() similar , The difference is that it encapsulates the qualified elements into an array and returns , There is only one who meets the conditions immediately , He also returns an array
var box2 = document.querySeletorAll(".box2");
边栏推荐
- Idea construit le projet jfinal + génération automatique de code + test de fonctionnement de la base de données (trois méthodes)
- Leetcode daily question 2021/12/6-2021/12/12
- LeetCode 每日一题 2022/1/24-2022/1/30
- Leetcode: 627. Change gender
- Jackson 解析 JSON 详细教程
- Numpy.reshape finish image cutting
- Leetcode: 197. rising temperature
- Centos7 installs MySQL 5.7 decompressed version & Navicat connection MySQL & firewall settings - the personal test is valid
- Bean 的作用域和生命周期
- LeetCode 每日一题 2021/12/20-2021/12/26
猜你喜欢
How to add funding information when writing a paper with latex
Help brand insight -- Analysis of consumers' emotional behavior
二、IDEA搭建JFinal項目+代碼自動生成+數據庫操作測試(三種方式)
Enumerate properties in objects
用指针遍历数组
CentOS7安装Mysql5.7解压版&Navicat连接Mysql&防火墙设置——亲测有效
Bean 的作用域和生命周期
Global scope and function scope
Programmer interview golden code interview question 01.04. palindrome arrangement
融云漫话:通信中台
随机推荐
Leetcode daily question 2021/11/22-2021/11/28
融云漫话:没有一个人躲得过“视频会议”
不再执着于用产品说话,苹果库克加大美国反垄断游说投入:今年上半年支出 460 万美元
Huawei mobile phone locking application
Kindling the Darkness: A Practical Low-light Image Enhancer
LeetCode 每日一题 2022/3/21-2022/3/27
Enumerate properties in objects
工业路由器油田无线监控
Flutter premier programme Hello world!
Bit and: the result of a number & 1
Mysql5.7 decompression configuration steps
[FatFs] porting FatFs file system based on STM32 SD card
MySQL execution process and sequence
Jackson parsing JSON detailed tutorial
Leetcode daily question 2022/2/21-2022/2/27
Numpy.reshape finish image cutting
Tcpdump simple usage
How to add funding information when writing a paper with latex
米哈游大量招募新同学,校招提前批最后一天!
MFC对话框程序只运行单个实例 的简单示例