当前位置:网站首页>JS foundation -- about DOM
JS foundation -- about DOM
2022-06-11 09:38:00 【Snatch bamboo shoots 123】
Catalog
DOM object
DOM Full name Document Object Model),W3C Recommended standard programming interface for handling extensible markup languages
DOM There are many ways , It provides methods to manipulate an element of a page , It can be understood as DOM The operation is HTML Element operation
DOM Use a tree structure , Each element in the page is represented as a tree node , Pictured 
Node type
DOM The Communist Party of China has 12 Types , Three common types are as follows
{ element plain section spot Belong to sex section spot writing Ben section spot \begin{cases} Element nodes \\ Attribute node \\ Text node \end{cases} ⎩⎪⎨⎪⎧ element plain section spot Belong to sex section spot writing Ben section spot
stay javascript We can pass nodeType Attribute to determine the type of a node , The different node attribute values are as follows
With a certain HTML Node as an example
<p id="demo1"> This is a paragraph </p>
| Elements | nodeType Property value | |
|---|---|---|
| Element nodes | <p></p> | 1 |
| Attribute node | id=“demo1” | 2 |
| Text node | This is a paragraph | 3 |
DOM Object traversal
Parent element
Get the parent element of an element
DOM object .parentNode
Subelement
Get all child nodes ( Include element nodes 、 Text node 、 Attribute nodes, etc )
| attribute | describe |
|---|---|
| childNodes | All child nodes |
| firstChild | First child node |
| lastChild | The last child node |
Get all element nodes ( Include only element nodes )
| attribute | describe |
|---|---|
| children | All child element nodes |
| firstElementChild | The first child element node |
| lastElementChild | The last child element node |
Brother element
Get all child nodes ( Include element nodes 、 Text node 、 Attribute nodes, etc )
| attribute | describe |
|---|---|
| previousSibling | Find previous sibling |
| nextSibling | Find the next sibling |
Get all element nodes ( Include only element nodes )
| attribute | describe |
|---|---|
| previousElementSibling | Find previous sibling element node |
| nextElementSibling | Find the next sibling element node |
How to get elements
get
| Method | describe | Return value |
|---|---|---|
| getElementById() | adopt id Locate an element | DOM |
| getElementsByTagName() | Locating elements by tag name | An array of class |
| getElementsByClassName() | Locate elements by class name | An array of class |
| getElementsName() | adopt name The attribute name locates the element | An array of class |
query
| Method | describe |
|---|---|
| querySelector(“ Selectors ”) | Select the first element that meets the condition |
| querySelectorAll(“ Selectors ”) | Select all elements that meet the conditions |
The selector is similar to css Selectors
Method of creating element
| Method | describe |
|---|---|
| createElement() | Create an element node |
| creatTextNode() | Create a text node |
How to manipulate elements
Insert
When we create an element node , You need to insert it into HTML file , This requires inserting elements
// take B Element inserted into A The end of the child element of the element
A.appendChild(B)
// take B Element inserted into A Child element of element C Before
A.insertBefore(B,C)
Delete
// Delete A Child element of element B
A.removeChild(B)
Replace
// The child element of the parent element B Replace with element C
A.replaceChild(B,C)
Set up HTML Internal attributes of the element
| attribute | describe |
|---|---|
| innerHTML | Gets or sets the internal element of an element |
| innerText | Gets or sets the internal text of an element |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> This is a html5 demo</title>
</head>
<body>
<script> window.onload=function(){
let new_html=document.createElement("p") new_html.className="the_new" new_html.style.backgroundColor="red" new_html.style.border="20px solid silver" new_html.innerText=" This is a paragraph " document.body.appendChild(new_html) // amount to // document.body.innerHTML='<p class="the_new" style="background-color:red; border:20px solid silver"> This is a paragraph </p>' } </script>
</body>
</html>
Chrome Browser run results 
obtain HTML Element attribute value
Get and set HTML The attribute value of the element
DOM object . Property value
// such as
//p.className Be careful
//p.type
//p.id
Object methods
| Method name | describe |
|---|---|
| getAttribute(“ Property name ”) | Get the attribute value of an element |
| setAttribute(“ Property name ”,“ Property value ”) | Set the value of an attribute of an element |
| removeAttribute(“ Property name ”) | Delete an attribute of an element |
| hasAttribute(“ Property name ”) | Determine whether an element contains an attribute |
css Property value operation
obtain CSS Property value
getComputedStyle(DOM object ). Property name
// or
getComputedStyle(DOM object )[ Property name ]
Such as
let a=getComputedStyle("p")["class"]
- Be careful :css Hump writing of attribute name
font-size>>fontSize
background-color>>backgroundColor- getComputedStyle Method to get css The advantages of attribute values
We know ,css There are three styles of inline 、 Inside 、 external , For HTML Elements , Three style settings CSS Style this method can get , But the following method can only get the first one, that is, intra line css style
Set up CSS Property value
style object
DOM object .style. Property name =" Property value "
// or
DOM object .style[ Property name ]=" Property value "
Such as
p.style.width=30%;
cssText attribute
Can be used for more than one CSS attribute
DOM object .style.cssText=" Property name : Property value ; Property name : Property value ,..."
边栏推荐
- Console you don't know
- When the enterprise makes a decision, which part should lead the ERP project?
- Type-C扩展坞自适应供电专利维权案例
- Why is it difficult to implement informatization in manufacturing industry?
- 从企业评价的方历来看ERP软件成功与失利
- The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
- Slice of go language foundation
- [software] ten skills to maximize the value of ERP system
- 报错ModularNotFoundError: No module named ‘find_version’
- ES6新增特性--箭头函数
猜你喜欢

Pytorch installation for getting started with deep learning

Redis source code analysis hash object (z\u hash)

关于原型及原型链

affair

Console you don't know

Method (common method), method execution memory analysis, method overloading mechanism, method recursion

Opencv image basic operation (III) -- image feature extraction (corner detection)

Openstack explanation (24) -- registration of neutron service

Day39 process object and other method mutexes

OpenCV OAK-D-W广角相机测试
随机推荐
Fabric.js 動態設置字號大小
[share] how do enterprises carry out implementation planning?
Suffix Array
Why is it difficult to implement informatization in manufacturing industry?
Day 47 how to query a table
js基础--运算符
js中关键字this的理解
ERP体系的这些优势,你知道吗?
Pytorch installation for getting started with deep learning
[TiO websocket] IV. the TiO websocket server implements the custom cluster mode
Openstack explanation (24) -- registration of neutron service
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
【分享】企业如何进行施行规划?
报错[error] input tesnor exceeds available data range [NeuralNetwork(3)] [error] Input tensor ‘0‘ (0)
Remote office related issues to be considered by enterprises
Machine learning notes - in depth Learning Skills Checklist
Development of PCBA circuit board for small oxygen generator
Comparison and introduction of OpenCV oak cameras
[scheme design] scheme of household oximeter based on single chip microcomputer
Runtimeerror: blobreader error:the version of imported blob doesn't match graph_ transformer