当前位置:网站首页>DOM operation -- get elements and nodes
DOM operation -- get elements and nodes
2022-07-25 02:49:00 【H5_ ljy】
List of articles
One 、 What is? DOM?
DOM yes document object model It is W3C The browser developed by the industry alliance is right for programmers HTML Interface for document operation
It translates the code in the document into an object model
In this model All relevant tag attributes, annotation text, etc 12 All kinds of data are translated into one type of object Collectively, node objects .
Two 、 How to get elements
1. The method provided by the system
| Method | describe |
|---|---|
| document.getElementById() | Through elements id To find the elements , Input “id name ” |
| document.getElementsByClassName() | Find elements by class name , Input " Class name " |
| document.getElementsByTagName() | Find elements by tag names , Input " Tag name " |
| document.getElementsByName() | adopt name Name to find the element , Input "name name " |
| document.querySelector() | Find matches specified CSS Selectors (id、 Class name 、 type 、 attribute 、 Attribute value and so on ) One of the first HTML Elements ; |
| document.querySelectorAll() | Find matches specified CSS Selectors (id、 Class name 、 type 、 attribute 、 Attribute value and so on ) All of the HTML Elements , Returns an array containing all the elements found |
Return value : see Element Did you add s, If yes, return an array containing all the elements found , If it is not found, an empty array is returned ; No addition s Then return the first element found , Otherwise null
<div id="div_1" class="div1">
<div class="div2"></div>
</div>
<div class="div1"></div>
<input type="text" name="1">
<input type="text" name="1">
<script>
console.log(document.getElementById('div_1'))
console.log(document.getElementsByClassName('div1'))
console.log(document.getElementsByTagName('div'))
console.log(document.getElementsByName('1'))
console.log(document.querySelector('div .div2'))
console.log(document.querySelectorAll('div'))
console.log(document.getElementById('div_3'),document.getElementsByClassName('div3'))
</script>

2. Direct access provided by the system
| Method | describe |
|---|---|
| document.anchors | Return ownership name All of properties <a> Elements . |
| document.body | return <body> Elements |
| document.documentElement | return <html> Elements |
| document.forms | Back to all <form> Elements |
| document.head | return <head> Elements |
| document.images | Back to all <img> Elements |
| document.inputEncoding | Return the encoding of the document ( Character set ) |
| document.lastModified | Return the date and time when the document was updated |
| document.links | Return ownership href All of properties <area> and <a> Elements |
| document.readyState | Return the ( load ) state |
| document.referrer | Returns the quoted URI( Link documents ) |
| document.scripts | Back to all <script> Elements |
| document.title | return <title> Elements |
| document.URL | Return the integrity of the document URL |
3. Get through relationship
| Method | describe |
|---|---|
| parentElement | Returns the parent element of the node that called it |
| parentNode | Returns the parent node of the node that called it |
| children | Returns an array containing all child elements |
| childNodes | Returns an array containing all child nodes |
| nextSibling | Return to the next sibling |
| nextElementSibling | Return to the next sibling element |
| previousSibling | Return to the previous sibling node |
| previousElementSibling | Return to the previous sibling element |
<div id="div_1" class="div1">
<div class="div2"></div>
</div>
<div id="div_2"></div>
<div class="div1"></div>
<script>
console.log(document.getElementsByClassName('div2')[0].parentElement)
console.log(document.getElementsByClassName('div2')[0].parentNode)
console.log(document.getElementsByClassName('div2')[0].parentElement.parentElement.parentElement.parentElement)
console.log(document.getElementsByClassName('div2')[0].parentNode.parentNode.parentNode.parentNode)
console.log(document.getElementById('div_1').children)
console.log(document.getElementById('div_1').childNodes)
console.log(document.getElementById('div_2').nextSibling)
console.log(document.getElementById('div_2').nextElementSibling)
console.log(document.getElementById('div_2').previousSibling)
console.log(document.getElementById('div_2').previousElementSibling)
</script>

Add :
The difference between elements and nodes :
Elements : namely html Inside the document , All tags can be called elements , for instance <div>、<p> etc. .
node : stay HTML DOM ( Document object model ) in , Every part is a node :
The document itself is a document node
all HTML An element is an element node
all HTML Attributes are attribute nodes
HTML The text within the element is a text node ( Including carriage return is also a text node )
Annotations are annotation nodes .
Their relationship is : Element is a kind of node , Called element nodes , But element nodes can contain many nodes .
parentElement and parentNode difference ::
The difference lies in html Labeled parentNode by document,parentElement There is no , return null;
4. Get how many child elements you are in the parent element / node
<div id="div_1" class="div1">
<div class="div2"></div>
</div>
<div id="div_2"></div>
<div class="div1"></div>
<script>
Object.prototype.indexOf_1=function () {
let parentChildren=this.parentElement.children; // Get the child elements of the caller's parent element
for(let i=0;i<parentChildren.length;i++){ // Loop the array returned in the previous step
if(parentChildren[i]==this){ // Determine which child element is the caller
return i // Returns the subscript of the caller in the child element set of the parent element
}
}
}
let div2=document.getElementById('div_2')
console.log(div2.indexOf_1())
边栏推荐
- The file in scanpy1.9.1 cannot be read in scanpy1.7.2. The problem is solved
- Analysis of FLV packaging
- Execution methods with static code blocks and child and parent classes
- [jailhouse article] scheduling policies and system software architectures for mixed criticality
- Keepalivetime=0 description of ThreadPoolExecutor
- Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template
- What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
- Sequence diagram of UML diagram series
- [pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
- Large number processing -- use case
猜你喜欢

Is it necessary to increase the number of milliseconds and save several KB of memory in the program?

JS foundation -- hijacking of this keyword
![[C language] program compilation (preprocessing)](/img/94/175a84d89b1f16987e529eb029cbbc.png)
[C language] program compilation (preprocessing)

Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)

Cookies and sessions

After six years of precipitation, new consumption has found a digital transformation paradigm

2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.

Details of C language compilation preprocessing and comparison of macros and functions

Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘

UDP message structure and precautions
随机推荐
JS foundation -- data
YuQue - a useful tool for document writing and knowledge precipitation
Unity refers to a variable in another class (its own instance)
"Introduction to interface testing" punch in to learn day07: websocket interface: how to test a completely unfamiliar protocol interface?
Componentization and modularization
[circular array] JS method collection of circular array
Map set learning
If there is a segment in the encryption field, are you "bronze" or "King"?
My creation anniversary (3rd Anniversary)
Picgo configuring Alibaba cloud OSS
Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
Arthas case: dynamic update application logger level
Sword finger offer 11. rotate the minimum number of the array
Conceptual distinction between Po, Bo, VO, dto and POJO
The latest interview questions and analysis of software testing in 2022
JS foundation -- math
Simulate the implementation of strstr
StrError and PERROR
Custom types in C language
Hyperchain hyperblockchain Shi Xingguo was interviewed by 36 krypton: the amount of customer cooperation consulting is doubling