当前位置:网站首页>DOM Foundation
DOM Foundation
2022-07-28 07:02:00 【Hahaha~】
One 、 Concept
- yes JavaScript operation HTML Document interface Its full name is Document object model (Doucment Object Model)
- effect : Translate the code in the document into an object model So you can use scripts to do all kinds of operations
- All relevant tag attributes, annotation text, etc. in the model 12 All kinds of data will be translated into one type of object Generally referred to as the Node object
- Browser according to DOM Model , take HTML The document is parsed into a series of nodes , Then these nodes form a tree structure
- DOM The smallest component unit of is the node (node), The tree structure of the document (DOM Trees ) from 12 There are three types of nodes
12 Types of nodes : Element nodes 、 Document nodes 、 Document type node 、 Document fragment node 、 Text node 、 Comment node 、 Characteristics of the node 、CDATA node 、 Entity name node 、 Entity reference name node 、 Processing instruction node 、DTD Declaration node
DOM Trees :

Two 、 How to get elements
( One ) The method provided by the system
The old way
- document.getElementById
<div id="box1"> The box box1</div>
<script>
var div1=document.getElementById("box1") // If not, go back null
console.log(div1) // Return a specific element
</script>- document.getElementsByClassName
<div class="box2"> The box 2-1</div>
<div class="box2"> The box 2-2</div>
<div class="box2"> The box 2-3</div>
<script>
var arr = document.getElementsByClassName("box2")
console.log(arr) // The return value is an array of classes It's a data container installed 3 A class named box2 Of div
</script>- document.getElementsByName
<input type="radio" name="rad">
<input type="radio" name="rad">
<input type="radio" name="rad">
<script>
var name = document.getElementsByName("rad")
console.log(name)
</script>- document.getElementsByTagName
<div> The box 1</div>
<div> The box 2</div>
<div> The box 3</div>
<script>
var tag = document.getElementsByTagName("div")
console.log(tag) // The return value is an array of classes It's a data container installed 3 individual div
</script>H5 A new method
- document.querySelector
<div id="box"> The box 1</div>
<div id="box"> The box 2</div>
<div id="box"> The box 3</div>
<script>
var el = document.querySelector("#box") // Select the first element that matches the selector Return on no null
console.log(el) // first div The box 1
</script>- document.querySelectorAll
<div id="box"> The box 1</div>
<div id="box"> The box 2</div>
<div id="box"> The box 3</div>
<script>
var els = document.querySelectorAll(".box2") // All elements that match the selector No return null
console.log(els) // All of the div The box 1、2、3
</script>notes :h5 The new method is better however document.getElementsById() It's the fastest
( Two ) The direct acquisition method provided by the system
- document.body //body label ( Commonly used )
- document.forms //form formset
- document.anchors //a Tag set
- document.images // Photo gallery
- document.links // Connection set
- document.URL // The URL of the current document
( 3、 ... and ) The method of obtaining through relation
Example html Code :
<div id="box1">
<div class="box2" id="box">2</div>
<div class="box2">
<div class="box3" id="box3">3</div>
</div>
<div class="box2" id="box4">4</div>
</div>- Parent The parent element and the parent node are the same
// With id by box Of div Benchmarking Get its parent node
var box = document.querySelector('#box')
var boxpe = box.parentElement // Parent element
var boxpd = box.parentNode // Parent node
- Sub level Child elements and child nodes are not necessarily the same
// With id by box1 Of div Benchmarking Get its child nodes
var box1 = document.querySelector('#box1')
var els = box1.children // Sub elements Is an array of classes Include 3 individual div Elements
console.log(els) //HTMLCollection(3) 0: div#box.box2 1: div.box2 2: div#box4.box2
var nodes = box1.childNodes // Children Is an array of classes Include 3 individual div Element nodes and document nodes
console.log(nodes) //NodeList(7) 0: text 1: div#box.box2 2: text 3: div.box2
// 4: text 5: div#box4.box2 6: text
With id by box Of div Benchmarking Get its child nodes
var box = document.querySelector('#box')
var bc = box.children
console.log(bc) //HTMLCollection(0) Return an empty data container - Brother node
Last brother : previousElementSibling Last brother element No return null
previousSibling Last sibling node No return null
// With id by box4 Of div Benchmarking Get its brother ( the previous ) node
var box4 = document.querySelector('#box4')
var bel = box4.previousElementSibling // Last element
console.log(bel) // class="box2" Of div
var bnode = box4.previousSibling // Last node
console.log(bnode) //#textNext brother : nextElementSibling Next brother element No return null
nextSibling Next sibling node No return null
// With id by box Of div Benchmarking Get its brother ( next ) node
var box = document.querySelector('#box')
var bne = box.nextElementSibling // The next element
console.log(bne)
var bns = box.nextSibling // Next node
console.log(bns)
// With id by box4 Of div Benchmarking Get its brother ( next ) node
var box4 = document.querySelector('#box4')
var bne = box4.nextElementSibling // The next element
console.log(bne) //null- The first 1 Child node
var box=document.getElementById("#box")
var fs1=box.firstChild // The first 1 Child node
var fs11=box.firstElementChild // The first 1 Sub elements
console.log(fs1,fs11) // "2" null- The last child node
var box4=document.getElementById("#box4")
var ls1=box4.lastChild // The last child node
var ls11=box4.lastElementChild // The last child element
console.log(ls1,ls11) // "4" null- Get how many child elements you are in the parent element / node
// Implement this method by yourself : The caller is the number of elements in the parent element .index()
Object.prototype.index1=function() {
console.log(this)
var arr=this.parentElement.childNodes
for (let i = 0;i<arr.length;i++) {
if(this==arr[i]){
return i
}
}
}
var index=document.getElementById("box").index1()
console.log(index)
// Another way
var son1=document.getElementById("box1").children[1] // The second child element
var son2=document.getElementById("box1").childNodes[1] // The second child node 边栏推荐
- Monotonic queue, Luogu p1886 sliding window
- Which is the best air conduction Bluetooth headset? Air conduction Bluetooth headset ranking
- 防火墙——iptables防火墙(四表五链、防火墙配置方法、匹配规则详解)
- iptables防火墙
- Repair the faulty sector
- DNS域名解析服务
- shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)
- Applet navigator cannot jump (debug)
- Animation animation realizes the crossing (click) pause
- Network - transport layer (detailed version)
猜你喜欢

2022 Tanabata gift recommendation! Nice, cheap and practical gift recommendation

shell脚本——编程条件语句(条件测试、if语句、case分支语句、echo用法、for循环、while循环)

DHCP服务

FTP服务

MySQL master-slave

Cocos2d-x learning notes Tile Map tiledmap

SSH服务配置

CentOS7部署MySQL数据库服务器

What's a good gift for Tanabata? Niche and advanced product gift recommendation

Shell script - "three swordsmen" awk command
随机推荐
Wechat applet custom compilation mode
Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones
KVM热迁移
Upload and download files from Ubuntu server
DNS forward resolution experiment
Insertion and deletion of nodes in linked list
[learning notes] drive
Blue bridge code error ticket
JSON notes
VSphere esxi 7.0 update 3 release notes
MySQL主从
FTP服务
Detailed explanation of LNMP construction process
Compilation and preprocessing of C language
MySQL master-slave
[learning notes] VIM editor
Network - data link layer
MOOC翁恺C语言第七周:数组运算:1.数组运算2.搜索3.排序初步
Network - transport layer (detailed version)
Esxi community network card driver updated again