当前位置:网站首页>[DOM] first knowledge of DOM
[DOM] first knowledge of DOM
2022-07-20 09:51:00 【Bukaisen, East Africa】
I've been studying recently JavaScript, I found that a lot of knowledge is learned and forgotten , This makes me feel a great frustration , therefore , I want to change my learning style , First take blogging as a record , To consolidate their own learning , Consider the past you shall know the future , Can go farther and farther . Blunt duck ~
Catalog :
1. What is? DOM?
2. Get elements
3. The basis of the event
4. Event advanced
5. Operational elements
6. Node operation
summary
One 、 What is? DOM?
Document object model (Document Object Model, abbreviation DOM), yes W3C The Organization recommends dealing with extensible markup languages (HTML perhaps XML) Standard programming interface .
1.dom Trees
- file : A page is a document ,DOM Use in document Express
- Elements : All tags in the page are elements ,DOM Use in element Express
- node : Everything in a web page is a node ,DOM Use in node Express
DOM Think of all the above as objects
Two 、 Get elements
2.1. Get page elements
The way | expression |
---|---|
according to ID obtain | document.getElementById(‘id’); |
Get... According to the tag name | element.getElementsByTagName(‘ Tag name ’); |
adopt HTML5 New method to get | 1. document.getElementsByClassName(‘ Class name ’);// Returns the collection of element objects according to the class name 2. document.querySelector(‘ Selectors ’); // Returns the first element object... Based on the specified selector 3. document.querySelectorAll(‘ Selectors ’); // Returns... According to the specified selector |
Special element acquisition | 1. doucumnet.body // return body Element object 2. document.documentElement // return html Element object |
3、 ... and 、 The basis of the event
3.1. Three elements of the event
- Event source —— The event is triggered by
- Event type —— How to trigger
- Event handler —— Through an anonymous function assignment
3.2. Perform the three steps of the event
- Get the event source
- Registration events , The binding event
- Add event handler ( Take the form of function assignment )
1. Registration events
1> Traditional way of registration
onclick = function() {
}
2> Methods listen to the registration method
addEventListener(' Event type ',{
} [ Capture ])
The third parameter defaults to or false For bubbling , If yes true Then capture
3.3. Delete event
onclick = null // Traditional way of registration
removeEventListener(' Event type ',fn) // Commonly used
3.4. Common mouse events
Mouse events | The trigger condition |
---|---|
.onclick | Mouse click |
.onmousedown / onmouseup | The mouse click Release and bounce |
.onfocus / onblur | Focus of attention / Lose focus |
.onmouseenter / onmouseleave | Mouse entry / Leave |
.onmouseover/onmousemove/onmouseout | Mouse over ( Enter range )/ Move / Leave |
3.5. Keyboard events
Keyboard events | The trigger condition |
---|---|
keydown | Press trigger |
keyup | Bounce trigger |
keypress | Press the key |
notes :keydown/keyup Case insensitive , Identify function keys
keypress Case sensitive , Can't recognize function keys
Four 、 Event advanced
4.1.DOM Flow of events
The event flow describes the order in which events are received from the page .
Events are propagated between element nodes in a specific order when they occur , This communication process is DOM Flow of events .
Three stages :
- Capture phase
- Current target stage
- bubbling phase
4.2.DOM Flow of events 2
- Event Bubbling :IE Put forward at the earliest , The event is received by the most specific element at the beginning , And then it goes up to DOM The process of the topmost node .
- Event capture : Netscape first proposed , from DOM Start at the top , Then it spreads down to the receiving process of the most specific elements step by step .
- Event delegation : principle : Not every child node sets up an event listener alone , Instead, the event listener is set on its parent node , Then the bubble principle is used to affect the setting at each child node .
4.3. Event object
- What is an event object ?
Official explanation :event Object represents the state of the event , For example, the state of keyboard keys 、 Mouse position 、 Mouse button status .
Simple understanding : After the event , A collection of information data related to an event is put into this object , This object is the event object event, It has many properties and methods .
eventTarget.onclick = function(event) {
}
eventTarget.addEventListener('click', function(event) {
})
// This event It's the object of the event , We also like to write e perhaps evt
- Common event objects
e.target and this The difference between :
this It's an event bound element , The caller of this function ( The element that binds this event )
e.target Is the element triggered by the event - Common mouse events
4. Mouse event object
3. Common keyboard events
4.4
5、 ... and 、 Operational elements
5.1. obtain 、 Modify element content
element.innerText
element.innerHTML
Difference one : Yes HTML Identification of labels
‘ It's today :’
innerText Don't recognize html label , Nonstandard
innerHTML distinguish html label ,W3C recommend
Difference two : Processing of spaces and line breaks
innerText The content obtained does not contain spaces
innerHTML The obtained content contains spaces
5.2. obtain 、 Modify element styles
element.style. Style name ( hump ) = ''
element.className = ''
Exclusive algorithm : If you have a set of elements , Want an element to achieve something
type , Need to use the exclusive algorithm of the loop , That is to exclude others first , then
Then set your own style .
for(var i=0;i<a.length;i++){
a[i].style.XXX = ''
}
5.3. obtain 、 Modify element properties
element. Property name = ''
5.4. form properties
input.value
// Ban
input.disabled
input.type
5.5. Custom properties
element.setAttribute(' Property name ',value)
element.getAttribute(' Property name ')
// Remove properties
element.removeAttribute(' Property name ')
5.6.[h5 newly added ][ie10 above ] data- The properties of the beginning :
element.dataset
element.dataset. Property name
element.dataset[' Property name ']
6、 ... and 、 Node operation
6.1. Node Overview
In a general way , Nodes have at least nodeType( Node type )、nodeName( The name of the node ) and nodeValue( Node values ) These three basic attributes .
- Element nodes nodeType by 1
- Attribute node nodeType by 2
- Text node nodeType by 3 ( The text node contains text 、 Space 、 Line break, etc )
We're actually developing it , Node operations mainly operate on element nodes
6.2. Node level
- Parent node
node.parentNode
- Child node
parentNode.childNodes( standard )
// a key
parentNode.children( Nonstandard )
// The following are incompatible
// Return to the first node
node.firstElementChild
// The last node
node.lastElementChild
// Returns the next sibling node of the current element
node.nextElementSibling
// Returns the previous sibling element node of the current element
node.previousElementSibling
6.3. Operation node
document.createElement(' Tag name ') // Create element nodes dynamically
Parent node .appendChild( New node ) // Append child node
Parent node .insertBefore( New node , Some child node ) // Insert before the specified child node Such as ul.children[0]( Append... Forward )
Parent node .removeChild( Child node ) // Delete child nodes Such as ul.children[0]( Delete the first )
node.cloneNode( Deep copy or not ) // Clone the current node true Deep copy ( label + Content )false Or empty is a shallow copy ( Only copy labels )
Three kinds of dynamic creation nodes
- document.write()
- element.innerHTML
- document.createElement()
difference :
**1. document.write It's a content stream that writes content directly to the page , But the document flow is finished , It causes the page to be redrawn completely
- innerHTML It's writing content into something DOM node , It doesn't cause the page to redraw completely
- innerHTML It's more efficient to create multiple elements ( Don't splice strings , Take the form of array splicing ), The structure is a little more complicated
- createElement() Creating multiple elements is a little less efficient , But the structure is clearer
summary : Different browsers ,innerHTML More efficient than creatElement high **
summary
About dom operation , We mainly focus on the operation of elements . It's mainly about creating 、 increase 、 Delete 、 Change 、 check 、 Attribute operation 、 Event operations .
establish
- document.write
- innerHTML
- createElement
increase
- appendChild
- insertBefore
Delete
- removeChild
Change
Major changes dom Element properties of ,dom Content of element 、 attribute , Form values, etc
- Modify element properties : src、href、title etc.
- Modify the content of common elements : innerHTML 、innerText
- Modify form elements : value、type、disabled etc.
- Modify element styles : style、className
check
- DOM Provided API Method : getElementById、getElementsByTagName Old usage Not recommended
- H5 New methods provided : querySelector、querySelectorAll promote
- Use node operations to get elements : Father (parentNode)、 Son (children)、 Brother (previousElementSibling、nextElementSibling) promote
Attribute operation
- setAttribute: Set up dom The attribute value
- getAttribute: obtain dom The attribute value
- removeAttribute Remove properties
Event operations
Register events for elements , take Event source . Event type = Event handler
Mouse events | The trigger condition |
---|---|
.onclick | Mouse click |
.onmousedown / onmouseup | The mouse click Release and bounce |
.onfocus / onblur | Focus of attention / Lose focus |
.onmouseenter / onmouseleave | Mouse entry / Leave |
.onmouseover/onmousemove/onmouseout | Mouse over ( Enter range )/ Move / Leave |
The road to learning is long , come on. !
Picking up flowers and birds is one of the interests , According to the long road of the moon wind .—— Wan ye
边栏推荐
- xilinx中的复位
- transforms_ List = [c_vision.decode(), c_vision.rescale (1.0 / 127.5, -1.0)] this is image standardization [-1,1], right?
- 小波分析之多分辨率分解和重构过程详解
- Let foreigners my brother him
- [mindspore] [mindrecord] save the reading problem after specifying the floating-point precision
- 基于飞凌NXP i.MX6ULL的无线网络测试
- When the vivado project version is upgraded, the relevant IP version IP status displays using cached IP results
- 瑞萨G2L核心板及开发板上手评测
- Jetson Xavier NX source code compilation and installation ROS melody experience (failed)
- FreeRTOS线程安全、中断安全的printf实现方式
猜你喜欢
基于循环卷积的一维小波变换程序验证(C语言)
基于飞凌NXP i.MX6ULL的无线网络测试
Install Oracle 11g and build database based on Linux operating system (centos7x without graphics)
Interpretation of semi supervised semantic segmentation with error localization network
FPGA刷题P4:使用8-3优先编码器实现16-4优先编码器、 使用3-8译码器实现全减器、 实现3-8译码器、使用3-8译码器实现逻辑函数、数据选择器实现逻辑电路
This paper interprets "PSCL hdeep: image-based protein localization using subcellular prediction integrated in human tissue"
i.MX8MP开发板移植USBWIFI RTL8192EU驱动
Debian系统移植USBWiFi RTL8192EU驱动并设置开机自启
用LS1028A开发板输出PWM方波
Interpretation of text compression aided transformer encoding
随机推荐
Vit structure
Redux 主要知识学习总结
FPGA network port implementation and detailed explanation (2)
Evaluate reading and writing speed and network of Reza rz/g2l storage
RS232标准DB9接口定义
FPGA 多数表决器(含代码)
1人天搞定9人天的日志接入开发——基于指令集物联网操作系统的项目开发实践
smplify
FPGA——用VGA时序显示图像原理详解(2)
基于循环卷积的一维小波变换程序验证(C语言)
stm32f4 用一个定时器输出多个不同频率占空比PWM波(含代码)
Qingdao Weifang becomes a rich man
基于飞凌NXP i.MX6ULL的无线网络测试
【复旦微】国产MCU学习(持续更新)
vivado工程版本升级时相关IP版本IP Status显示Using cached IP results
Vivado2018.2 version with PS side configuration (BD) when calling Modelsim simulation: (vlog-13006) could not find the package (sc_util_v1_0_3_pkg)
MNN learning notes
Interpretation of semi supervised semantic segmentation with error localization network
RTOS——桌面mini网络时钟
STM32f4 使用高级定时器输出PWM波(含代码)