当前位置:网站首页>Summary of DOM knowledge points
Summary of DOM knowledge points
2022-06-30 00:09:00 【Taste of saury】
DOM
DOM yes JS Interface to operate web page , Its full name is “ Document object model ”(Document Object Model). Its function is to turn the web page into a JS object , So you can use scripts to do all kinds of operations
file : A document is a representation of the whole html Web documents
object : Object means to convert every part of a web page into an object
Model : Use models to represent relationships between objects , This makes it easy for us to get objects
Common node
The type of node :Document, DocunentType,Element,Text,Comment,DocumentFragment,
Common nodes :
Document nodes (document)
Whole html file document Object as window The properties of the object exist , Can be used directly without obtaining
Element nodes (Element)
In document html label
Attribute node (Attribute)
Attribute of element Represents the attributes of each tag , The attribute node is not a child of the element node , Is part of the element node
Text node (Text)
html Text content in Tags
All nodes of a document , According to your grades , It can be abstracted into a tree , This tree is DOM
Parent node relationship (parentNode): The direct parent node
Child node relationship (childNode): Direct child nodes
Peer node relationship (sibling): Nodes with the same parent node
Document type
documentElement Always point to html Page type <html> Elements
body Direct to <body> Elements
title Get the title of the document
document.URL To achieve complete URL
getElementByld Method
visit html The most common way to use an element is to use the id
innerHTML attribute
The easiest way to get element content is to use innerHTML attribute
| Method | attribute | |
| document.getElementByid () | Through elements id To find the elements | |
| document.getElementsByTagName() | Find elements by tag names | |
| document.getElementsByClassName() | Find elements by class name | |
| document.querySelector() | Returns the document that matches the specified CSS The first element of the selector | |
| document.querySelectorAll() |
|
getElementById()
Return match specification id An element of
<div id="d1"> I am a div label </div>
<script>
// lookup id by d1 The label of
var div = document.getElementById('d1');
console.log(div);
</script>getElementsByTagName()
Return to one htmlcollection( Pseudo array ), Contains all elements that match the specified tag name
<p> I am a p label </p>
<p> I am a p label </p>
<p> I am a p label </p>
<script>
// Find all p label
var p = document.getElementsByTagName('p');
console.log(p);
</script>getElementsByClassName() Return to one HTML aggregate HTMLCollection( Pseudo array ), Contains all elements that match the specified class name .
<div class="div1"> I am a div label </div>
<div class="div1"> I am a div label </div>
<div class="div1"> I am a div label </div>
<script>
// lookup class by div1 The label of
var div = document.getElementsByClassName('div1');
console.log(div);
</script>document.querySelector() Returns the document that matches the specified CSS The first element of the selector
<div id="div1"> I am a div</div>
<div id="div1"> I am a div</div>
<script>
document.querySelector("#div1").innerHTML = "Hello World!";
</script>document.querySelectorAll() document.querySelectorAll() yes HTML5 New methods introduced in , Returns the matching in the document CSS List of all element nodes of the selector
<div class="div1"> I am a div</div>
<div class="div1"> I am a div</div>
<script>
console.log(document.querySelectorAll(".div1"));
var x = document.querySelectorAll(".div1");
x[0].innerHTML = ' I'm new div';
</script> Additive elements
Create a new HTML Elements , To work with appendChild() or insertBefore() Methods in combination . among ,appendChild() Method to add a new child node at the end of the node's child node list .insertBefore() Method to insert a new node anywhere in the node's child node list .
<script>
// Create element node p
var p = document.createElement('p');
// towards p Label insert
p.innerHTML = ' I am a p label ';
// Insert the node into body in
document.body.appendChild(p);
</script>document.write() Write to a document, such as text or HTML expression
<script>
document.write("<p>Hello world!</p>");
document.write("<span>Hello DOM!</span>");
document.write("Hello Weekend!");
</script>attributes: Returns a collection of all attributes related to the element
classList: Returns the class Collection of properties
className: Gets or sets the of the specified element class The value of the property
clientHeight: Gets the height inside the element , Contains the inside margin , Horizontal scrollbars are not included 、 Border and margin .
clientTop: Returns the height of the element from its upper boundary
clientLeft: Returns the width of the element from its left boundary
clientWidth: Returns the width of the element inside it , Including the inside margin , But it doesn't include vertical scrollbars 、 Border and margin .
innerHTML: Set or get HTML The descendants of elements represented by syntax
Text attributes and methods
Text length length
Add text appendData(text)
Delete text deleteData(beginIndex,count)
insert text insertData(beginIndex,text)
replace text replaceData(beginIndex,count,text)
<div id="container"></div>
<script> // Create text node
var textNode = document.createTextNode('Hello World!'); // obtain container
var div = document.getElementById('container'); // Insert a text node into container div.appendChild(textNode); // replace text
textNode.replaceData(0,5,'Hi'); // insert text
textNode.insertData(0, 'Hello')
</script>
边栏推荐
- Halcon practical: design idea of solder joint detection
- What is flush software? Is it safe to open an account online?
- 漫画安全HIDS、EDR、NDR、XDR
- Basic tutorial for installing monggodb in win10
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
- 6.29 problem solving
- 6.28 problem solving
- Sword finger offer 15 Number of 1 in binary
- AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
- Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
猜你喜欢

设置安全组、域名备案、申请ssl证书

Leetcode (680) -- verifying palindrome string II

Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)

Andorid source build/envsetup. SH details to know
500 error occurred after importing skins folder into solo blog skin

云原生爱好者周刊:炫酷的 Grafana 监控面板集合

Halcon practical: design idea of solder joint detection

Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils

架构实战营模块5作业

EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended
随机推荐
Xutils3 transfer set
Web APIs 环境对象 丨黑马程序员
Halcon practical: design idea of solder joint detection
shell-运算符
如何实现搜索引擎中的拼写纠错功能——思路
[wechat applet] understand the basic composition of the applet project
Et la tarte aux framboises 4? Quels sont les jeux possibles?
[rust weekly library] Tokei - a utility for statistics of code lines and other information
旋转彩色三叶草
Solr基础操作15
6.28日刷题题解
MySQL functions and constraints
Unity splashimage scaling problem
QT learning 04 Hello QT
Golang6 reflection
This PMP Exam (June 25), some people are happy and others are worried. That's why
请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
Cloud native enthusiast weekly: cool collection of grafana monitoring panels
Is China Merchants Securities reliable? Is it safe to open a stock account?
History of deep learning