当前位置:网站首页>JS foundation 8
JS foundation 8
2022-06-28 11:43:00 【Programmer community】
DOM Document object model
Definition
Provide operation HTML Document object capability , Provide API
operation
- Get tag elements
- Operation content
- Operation style
- Operation properties
DOM object
- document.title
- document.write()
<title>Document</title>
<script>
var box=document.title
console.log(box)
// result Document
document.write('hello word')
// result hello word
</script>
Get tag elements
| attribute | meaning |
|---|---|
| document.getElementById(’’) | adopt ID obtain |
| document.getElementsByClassName(’’) | adopt CLASS obtain |
| document.getElementsByTagName(’’) | Get... Through tags |
| getElementsByName(’’) | adopt NAME obtain |
| document.querySelector(’’) | image CSS Sample acquisition |
| document.querySelectorAll(’’) | image CSS Sample acquisition |
notes :
- getElementsByName(’’) Used to get form elements name
- Only document.getElementById(’’) and document.querySelector(’’) To get a single value , Others are pseudo arrays
<div id="box">box1</div>
<div class="box">box2</div>
<p>p1</p>
<input type="text" name="text">
<script>
var box1=document.getElementById('box')
var box4=document.querySelector('#box')
var box5=document.querySelector('.box')
var box6=document.querySelectorAll('div')
var box2=document.getElementsByClassName('box')
var box3=document.getElementsByTagName('div')
var input=document.getElementsByName('text')
console.log(box1)
// result <div id="box">box1</div>
console.log(box2)
// result [div.box]
console.log(box3)
// result [div#box, div.box, box: div#box]
console.log(input)
// result [input]
console.log(box4)
// result <div id="box">box1</div>
console.log(box5)
// result <div class="box">box2</div>
console.log(box6)
// result [div#box, div.box]
</script>
Operation content
- innerHTML Get text content , Including the tag elements inside
- innerText Get text content , Do not include the tag elements inside , You can only get plain text
- value Get text content , You can only get text (input) What's in it
<div>
<div> Test text </div>
</div>
<script>
var divEle=document.querySelector('div')
var content1=divEle.innerHTML
var content2=divEle.innerText
console.log(content1);
// result <div> Test text </div>
console.log(content2);
// Result test text
</script>
Set contents
- Ele.innerHTML=‘ Content ’
- Ele.innerText=‘ Content ’
Operation style
| attribute | meaning |
|---|---|
| Ele.style.color=‘red’ | Add inline style |
| Ele.className=‘ Class name ’ | Add class name |
| Ele.classList.add(‘ Class name ’) | Add class name |
| Ele.classList.remove(‘ Class name ’) | Remove class name |
<div></div>
<script>
var divEle=document.querySelector('div')
divEle.style.backgroundColor='red'
console.log(divEle);
// result <div style="background-color: red;"></div>
divEle.className='box'
console.log(divEle);
// result <div class="box" style="background-color: red;"></div>
divEle.classList.add('box2')
console.log(divEle)
// result <div class="box box2" style="background-color: red;"></div>
divEle.classList.remove('box2')
console.log(divEle);
// result <div class="box" style="background-color: red;"></div>
</script>
Operation properties
- Set properties
setAttribute(‘ Property name ’,‘ Property value ’) - get attribute
getAttribute(‘ Property name ’) - Remove properties
removeAttribute(‘ Property name ’)
<div></div>
<script>
var divEle=document.querySelector('div')
divEle.setAttribute('class','box')
console.log(divEle);
// result <div class="box"></div>
var ToObtain=divEle.getAttribute('class')
// result box
var ToObtain=divEle.removeAttribute('class')
console.log(ToObtain);
// result <div></div>
</script>
Find node

DOM Node object
- Whole HTML A document is treated as a document object document
- Whole HTML Label elements are treated as DOM Node object
- Every HTML The content attribute of the tag element is also regarded as DOM Node object
notes : There are blank text nodes between element nodes
Dynamic operation node
- Create nodes
- Create element node
var elment=document.createElenment('div') - Create text node
var textNode=document.createTextNode(' Elements ')
- Add a node
- Add child nodes to the element
Parent node .oppendchild(' Child node ') - Insert the child node specified in the parent element
Parent node .insertBefore(' New child nodes ', Old child nodes )
- Delete node
Delete the child node under the parent nodeParent node .removeChild(' Child node ')Child node .remove() - Replacement node
The new node replaces the child node under the parent nodeParent node .replaceChild(' New node ',' Old node ')
<div></div>
<script>
var elmentNode=document.createElement('div')
// Create element node
var textNode=document.createTextNode(' Test creation element ')
// Create text elements
elmentNode.appendChild(textNode)
// The text element is added to the node
var node=document.querySelector('div')
node.appendChild(elmentNode)
// result <div>
<div> Test creation element </div>
</div>
elmentNode.remove()
// result <div></div>
</script>
Clone node
node .clone()
node node
All node types must inherit node type , Become node A subclass or descendant of
| attribute | nodeType | nodeName | nodeValue |
|---|---|---|---|
| Elements | 1 | Upper case label name | null |
| Text | 3 | #text | Content |
| attribute | 2 | Property name | Property value |
Get non line style
window.getComputedStyle( Node object ). Style name
<style> div{
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div></div>
<script>
var divEle=document.querySelector('div')
var accept=window.getComputedStyle(divEle).width
console.log(accept)
// result 100px
</script>
Offset
- offsetTop
Distance above the parent element - offsetLeft
Distance above the parent element
notes : No positioning and window , There is a parent element positioned
Get the element width
| attribute | meaning |
|---|---|
| window.getComputedstyle( Node object ). Style name | Content |
| clientWidth | Wide content +padding |
| offsetWidth | Wide content +padding+border |
<style> div{
width: 100px;
height: 100px;
padding: 10px;
border: 10px solid #000;
}
</style>
</head>
<body>
<div></div>
<script>
var divEle=document.querySelector('div')
var content=window.getComputedStyle(divEle).width
console.log(content)
// result 100px
var contentPadding=divEle.clientWidth
console.log(contentPadding);
// result 120
var contentBorder=divEle.offsetWidth
console.log(contentBorder);
// result 140
</script>
边栏推荐
- 2022中国信通院首届业务与应用安全发展论坛成功召开!
- day39 原型链及页面烟花效果 2021.10.13
- Excel导入导出便捷工具类
- 东方财富手机股票开户哪个券商更安全更方便?
- Which broker is safer and more convenient to open an account for Oriental Fortune mobile stock?
- 李宏毅《机器学习》丨7. Conclusion(总结)
- Timestamp and date conversion "suggested collection"
- Intranet penetration in the working group environment: some basic methods
- 网页提示此站点不安全解决方案
- 董宇辉,新东方以及凤凰卫视
猜你喜欢

Day30 JS notes BOM and DOM 2021.09.24

Day31 JS notes DOM 2021.09.26

Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community

QML control type: tabbar

Fancy features and cheap prices! What is the true strength of Changan's new SUV?

day29 js笔记 2021.09.23

QML控件类型:TabBar

Scientific research - web of science retrieval skills

Redis6 1: what problems can be solved by the introduction of NoSQL and redis?

行业分析| 快对讲,楼宇对讲
随机推荐
MySql5.7添加新用户
Wireshark数据抓包分析之FTP协议
Mysql使用max函数查询不到最大值
关于Pytorch中双向LSTM的输出表示问题
SQL必需掌握的100个重要知识点:检索数据
Array method in JS 2021.09.18
day32 js笔记 事件(上)2021.09.27
day31 js笔记 DOM下 2021.09.26
Excel import / export convenience tool class
day33 js笔记 事件(下)2021.09.28
合约量化交易系统开发 | 合约量化APP开发(现成案例)
Excel导入导出便捷工具类
工作组环境下的内网渗透:一些基础打法
Web page tips this site is unsafe solution
元宇宙系统的发展与原理介绍
Introduction to GDB
如临现场的视觉感染力,NBA决赛直播还能这样看?
Redis6 一:Nosql引入、Redis可以解决什么问题?
day34 js笔记 正则表达式 2021.09.29
JS foundation 3