当前位置:网站首页>Web API - steps and operation elements for executing events
Web API - steps and operation elements for executing events
2022-07-16 06:07:00 【mintsolace】
Steps to execute the event
1. Get the event source
var div = document.querySelector('div');
2. Registration events ( The binding event )
div.onclick
3. Add event handler ( Take the form of function assignment )
div.onclick = function (){
console.log('checked');
}
Javascript Of DOM The operation can change the content of the web page 、 Structure and pattern , You can use DOM Manipulate elements to change the contents of elements 、 Properties, etc .
1. Change element content
element.innerText: Content from start to end , But it removes html label , At the same time, spaces and line breaks are removed .
element.innerHTML: Everything from the start position to the end position , Include html label , Keep spaces and line breaks at the same time .
innerText and innerHTML difference
innerText Don't recognize html label , Nonstandard , Remove spaces and line breaks
innerHTML distinguish html label , accord with W3C standard , Keep spaces and line breaks
var div = document.querySelector('div');
div.innerText = '<strong> It's today </strong>: 2020';
div.innerHTML = '<strong> It's today </strong>: 2020';
These two properties are readable and writable , You can get the content of the element
2. Attribute operation of common elements
innerText、innerHTML Change element content
src、href
id、alt、title
<body>
<button id="wyx"> Wei Yuxuan </button>
<button id="wxy"> Wang Xinyu </button><br>
<img src="images/wyx.jpg" alt="" title = "wyx">
<script> // Modify element properties src //1. Get elements var wyx = document.getElementById('wyx'); var wxy = document.getElementById('wxy'); var img = document.querySelector('img'); //2. Registration events The handler wxy.onclick = function (){
img.src = 'images/wxy.jpg'; img.title = 'wxy' } wyx.onclick = function (){
img.src = 'images/wyx.jpg'; img.title = 'wyx'; } </script>
</body>
3. Attribute operations of form elements
utilize DOM You can manipulate the attributes of the following form elements :
type、value、checked、selected、disabled
If you want a form to be disabled , No more clicks , use disabled, Put this button button Ban :
//btn.disabled = true;
this.disabled = true; //this It points to the caller of the event function
JD password box case
(1) The core idea : Click the eye button , Change the password box type to a text box to see the password inside
(2) One button, two states , Click once , Switch to text box , Continue clicking once to switch to the password box
(3) Algorithm : Using a flag Variable to judge flag value , If it is 1 Just switch to text box ,flag Set to 0, If it is 0 Just switch to the password box ,
//1. Get elements
var eye = document.getElementById('eye');
var pwd = document.getElementById('eye');
//2. Register event handlers
var flag = 0;
eye.onclick = function(){
// After one click ,flag Be sure to change
if (flag == 0){
pwd.type='text';
eye.src = 'images/open.png';
flag=1;// Assignment operation
}else{
pwd.type = 'password';
eye.src = 'images/close.png';
flag=0;
}
}
4. Style property operation
It can be done by JS Modify element size , Color , Position, etc
element.style: Inline style operation
element.className: Class name style operation
(1)JS The style inside adopts the hump naming method , such as fontSize、backgroundColor
(2)JS modify style Style operation , What's produced is the in line style ,css The weight is relatively high
Close Taobao QR code case
<div class="box">
Taobao QR code
<img src = "images/tao.png" alt="">
<i class="close-btn">x</i>
</div>
<script>
//1. Get elements
var btn = document.querySelector('.close-btn');
var box = document.querySelector('.box');
//2. Register event handler
btn.onclick = function(){
box.style.display = 'none';
}
</script>
The box is just hidden , Not deleted
边栏推荐
- For some problems encountered in using crontab, errors are reported /var/spool/cron: permission denied and bash: /usr/bin/chattr: permission denied
- 曾入选顶会的技术完成产品化 蚂蚁链推出版权AI计算引擎
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- 自定义Loading动画
- FTP upload file script description
- 利用RAC实现验证码发送逻辑
- Unity experiment - control the movement of game objects
- Calculate the number of days difference between localdates, which is convenient and fast
- MySQL-多表查询-内连接/外连接/自连接
- Tkmapper uses weekend splicing conditions to query conditions
猜你喜欢

网络通信安全部分笔记一

Intranet penetration notes --:) a smiling face

【MIT Missing Semester 2】Shell Tools

JS downloads files according to binary data
利用Spark预测回头客实验报告

Nftscan Developer Platform launches Multi Chain NFT data pro API service

chrome浏览器91版本SameSite by default cookies被移除后的解决方案,Chrome中跨域POST请求无法携带Cookie的解决方案

Intranet penetration notes - vulnhub intranet course completion penetration

微信小程序开发二三事

Unity experiment - control the movement of game objects
随机推荐
JS time object
Unity experiment - simulating the motion of stars in the solar system
Intranet penetration notes - MSF
Solutions to Oracle database error codes
JS scope chain
防抖与节流:实践与勘误
ES6--Set
Open source internship | compiler sig internship task is officially released. Welcome to apply!
Experimental report on using spark to predict repeat customers
编译原理-语法分析器设计
ES6 -- let and Const
MySQL-多表查询-联合查询/子查询
Notes 2 of network communication security
Go简明语法汇总--入门
How to solve the "Impossible Triangle" problem of data flow?
[Huang ah code] redis realizes fuzzy query and deletes | redis obtains the key according to the prefix
Euler talk | developer community experience bureau starts at about 19:30 on July 14
unity实验-模拟太阳系星体运动
扶朕起来,朕还能接着编!
微信小程序开发二三事