当前位置:网站首页>Web API、DOM
Web API、DOM
2022-06-11 06:55:00 【A Xuan is going to graduate~】
One :WEB API and js The associated .
1.js It is divided into ECMAScript and DOM、BOM.js Fundamental is ECMAScript, Mainly js Basic grammar .WEB API The main thing to learn is DOM and BOM.

js Based on learning ECMAScript Basic grammar paves the way for the following ,WEB APIs yes JS Application , A lot of use JS Basic grammar to do interactive effects .
2.API and WEB API
(1)API
Simple understanding :API Is a tool provided by programmers , In order to realize the desired function more easily .
(2)WEB API


Two 、DOM Reading guide
2.1 DOM brief introduction

2.2 DOM Some proper nouns of :
DOM Trees :

file : A page is a document .DOM Use in doucument Express
Elements : All tags in the page are elements ,DOM Use in element Express
node : Everything in the page is a node ( label 、 attribute 、 Text 、 Notes, etc. ),DOM Use in node Express .
2.3 Get elements
There are several ways to get the elements in the page :
2.3.1 according to ID obtain .
Use getElementById(); To get
(1) Because the document page loads from top to bottom , So first you have to have a label . therefore script Write it under the label .
(2) Parameters id Is a case sensitive string
(3) It returns an element object , Can pass typeof Variable name Check it out. , Back to a object
(4)console.dir( Variable name ) Print the elements of the object Better view the properties and methods inside .
<body>
<div id="time">2010-9-9</div>
<script>
var timer = document.getElementById('time');
console.log(timer);
console.log(typeof timer);
console.dir(timer);// Output timer Properties and methods of .
</script>
</body>2.3.2 Get... According to the tag name
Use getElementsByTagName() Method can return a with the specified tag name A collection of objects .
(1) What is returned is the collection of obtained element objects , With Pseudo array Form storage of
(2) You want to print the element object in it , You can use traversal output . The resulting element object is dynamic .
give an example :① hypothesis ul Multiple... Are stored in li Elements , Want to get multiple li Elements .
have access to document.getElementsByTagName('li'); Get... In the entire document li Elements .
② hypothesis ul Store multiple... In li Elements ,ol Multiple... Are also stored in li Elements .
If you want to get the li, have access to document.getElementsByTagName('li');
- If you want to get ol Under the li, have access to element.getElementsByTagName('li'). First of all get var ol = document.getElementsByTagName('ol'); then console.log(ol[0].getElementsByTagName('li')).// Premise , There is only one... In the whole page ol. Why use [0] Well , because ol Get an array , Not an element , So write it [0]
- Or to ol Add one more id, Through the first var ol = document.getElementById(id name ), Re pass ol.getElementsByTagName('li') obtain li Elements .
2.3.3 Get elements by class name . Be careful :H5 Newly added .
getElementsByClassName()// Returns the collection of element objects according to the class name
2.3.4 Select by selector , It's also H5 Newly added .
(1)document.querySelector(' Selectors ')// According to the specified selector The first element object . Remember that the selector inside needs to be signed .
- document.querySelector('. Class name ')
- document.querySelector('#id')
- document.querySelector(' Tag name ')
(2)document.querySelectorAll(' Selectors ')// Returns all element objects of the specified selector .
2.4 Get special elements (body、html)
Can be set by id To get , however body and html There is only one page . Therefore, the method is also provided .
2.4.1 obtain body Elements .
var bodyEle = document.body
2.4.2 obtain html Elements
var htmlEle = document.documentElement;
2.5 event
A mechanism for triggering a response , Every element in a web page can trigger js event .
2.5.1 Composition of events ( Event source 、 Event type 、 Event handler Also known as the three elements of events )
(1) Event source : The event is triggered by
(2) Event type : How to trigger , What event : Mouse click (onclick)、 Mouse over 、 Roller, etc
(3) Event handler : Through a function assignment to complete
Case study :
<body>
<button id = "btn"> Tang Bohu </button>
<script>
var btn = documnent.getElementById('btn');// Event source
btn.onclick = function(){// Event type 、 Three elements of the event
alert(' Some autumn fragrance ');
}
<script>
</body>2.5.2 Steps to execute the event :
- Get the event source
- The binding event ( Registration events )onclick
- Add event handler btn.onlick = function(){.....}

2.6 Operational elements
2.6.1 Modify element content

<body>
// Press the button ,div Display the current system event time ,p The tag directly displays the current system event .
<button> Displays the current system event </button>
<div> Display time </div>
<p></p>
<script>
var btn = document.querySelector('button');
var div = document.quertSelector('div');
btn.onclick = function(){
div.innerText = '2019-06-06';// Or use the previously written function to get the current system event .
}
var p = document.querySelector('p');
p.innerText('2019-06-06')
</script>
</body>innerText and innerHTML The difference between :
They are both readable and writable .
When writing :
(1)innerText Don't recognize html label . If you write with html label , Will write it out . Nonstandard
(2)innerHTML distinguish html label . If you write , with html label , For example, make the font bold , It will recognize , And make the font bold . W3C standard .
When reading :
(1)innerText No display HTML label . Remove spaces and line breaks at the same time . Nonstandard
(2)innerHTML Show html label . Keep spaces and wrap .W3C standard .
2.6.2 Modify element properties
<body>
// Click on different buttons , Show different pictures
<button id="ldh"> Lau Andy </button>
<button id="zxy"> Jacky Cheung </button>
<img src="images/ldh.jpg" alt="" title=" Lau Andy ">
<script>
// modify img in src Properties of
//1. Get elements
var ldh = document.getElementById('ldh');
var zxy = document.getElementById('zxy');
var img = document.querySelectors('img');
//2. The binding event
zxy.onclick = function(){
img.src = 'images/zxy.jpg';
img.title = ' Lau Andy ';
}
ldh.onlick = function(){
img.src = 'images/ldh.jpg';
img.title = ' Jacky Cheung ';
}
</script>
</body>Case study : Time sharing displays different pictures .( I have done some exercises . Pay attention to the problems encountered .innerText It's not a function , It's an attribute .)
2.6.3 Attribute operations of form elements
Form element properties :<input type="" value=" Input content " checked selected disabled>
<body>
<button> Button </button>
<input type="text" value=" Input content ">
<script>
var btn = document.querySelector('button');
var input = document.querySelector('input');
btn.onclick = function(){
input.value = ' By clicking the ';
// If a form is disabled . No more clicks , It can be used disabled This attribute . Can be button Ban
//btn.disabled = true; Both writing methods can be .
this.disabled = true;//this It points to the caller of the event function .
}
</script>
</body>Case study : Imitation Jingdong shows and hides password plaintext cases
Imitation Jingdong show hidden password plaintext case code and ideas
https://blog.csdn.net/xuan971130/article/details/1232108832.6.4 Modify style properties

<head>
<style>
div{
width: 200px;
height:200px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
var div = document.querySelector('div');
div.onclick = function(){
this.style.backgroundColor = 'red';
}
</script>
</body>- js The style in adopts the hump naming method : such as fontSize、backgroundColor
- js modify style Style operation , In line style ,css The weight is relatively high .js Style modified in , Will be promoted to the inline style , The weight is relatively high .
Case study 2: The case of imitating Taobao relationship QR code
Imitation Taobao closed QR code ideas and code
https://blog.csdn.net/xuan971130/article/details/123212449 Case study 3 Imitation Taobao shows the spirit figure
边栏推荐
- 迅为干货 |瑞芯微RK3568开发板TFTP&NFS烧写(上)
- 22年五月毕设
- About parseint()
- Resolve typeerror: ctx injections. tableRoot.$ scopedSlots[ctx.props.column.slot] is not a function
- 数学方法论的含义和研究意义
- []==![]
- .NET C#基础(6):命名空间 - 有名字的作用域
- Common troubleshooting tools and analysis artifacts are worth collecting
- VTK vtkplane and vtkcutter use
- Leetcode hot topic 100 topic 11-15 solution
猜你喜欢

Redux learning (III) -- using Redux saga, writing middleware functions, and splitting reducer files

The realization of online Fox game server room configuration battle engagement customization function
![[]==! []](/img/65/ab724c74b080da319ed5c01c93fdb7.png)
[]==! []

byte和bit的区别

VTK-vtkPlane和vtkCutter使用

During unity panoramic roaming, AWSD is used to control lens movement, EQ is used to control lens lifting, and the right mouse button is used to control lens rotation.

JVM from getting started to abandoning 1: memory model

Henan college entrance examination vs Tianjin college entrance examination (2008-2021)

Drawing with qpainter

About the principle and code implementation of Siou (review IOU, giou, Diou, CIO)
随机推荐
Do you use typescript or anyscript
Mongodb installation
The difference between TCP and UDP
开源漫画服务器Mango
搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
【Matlab印刷字符识别】OCR印刷字母+数字识别【含源码 1861期】
Flutter 内外边距
sql查询问题,只显示列名 不显示数据
Transformer Tracking
Common modules of saltstack
【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题
Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand
How exactly does instanceof judge the reference data type!
Reconstruction and preheating of linked list information management system (2) how to write the basic logic using linear discontinuous structure?
The nearest common ancestor of 235 binary search tree
021 mongodb database from getting started to giving up
Unity 全景漫游过程中使用AWSD控制镜头移动,EQ控制镜头升降,鼠标右键控制镜头旋转。
22年五月毕设
【Matlab图像加密解密】混沌序列图像加密解密(含相关性检验)【含GUI源码 1862期】
品牌定位个性六种形态及结论的重大意义