当前位置:网站首页>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>
边栏推荐
- JVM之栈空间
- History of deep learning
- koa2学习和使用
- Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
- 旋转彩色三叶草
- 项目一:部署 LAMP ecshop电商平台
- Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}
- 请指教同花顺软件究竟是什么?究竟网上开户是否安全么?
- Sword finger offer 14- I. cut rope
- Andorid source build/envsetup. SH details to know
猜你喜欢

The role of VMware virtual machine

项目一:部署 LAMP ecshop电商平台

Matlab exercises -- program control process exercise

There is no web-based development for the reward platform. Which is suitable for native development or mixed development?

Sword finger offer 14- I. cut rope

【微信小程序】认识小程序项目的基本组成结构

Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
![[Shangshui Shuo series] day 8](/img/66/2aaa82f122612db1775bdd45556d97.png)
[Shangshui Shuo series] day 8

QT learning 07 coordinate system in QT

Introduction to reptiles: data capture of Betta barrage, with 11 introductory notes attached
随机推荐
漫画安全HIDS、EDR、NDR、XDR
这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
500 error occurred after importing skins folder into solo blog skin
[Shangshui Shuo series] day 8
MySQL:SQL概述及数据库系统介绍 | 黑马程序员
西门子低代码 9.14版本: 满足不同需求
MySQL functions and constraints
6.29 problem solving
旋转彩色三叶草
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
Solr基础操作6
Virtual machine online migration based on openstack
Basic tutorial for installing monggodb in win10
What is IGMP? What is the difference between IGMP and ICMP?
Cartoon security HIDS, EDR, NDR, XDR
QT learning 06 widgets and window types
Solr基础操作14
【微信小程序】认识小程序项目的基本组成结构
Copy linked list with random pointer [space for time --hash record]
Unity about failure (delay) of destroy and ondestroy