当前位置:网站首页>Web APIs environment object - dark horse programmer
Web APIs environment object - dark horse programmer
2022-06-30 00:04:00 【Dark horse programmer official】
Notes updated in the early stage :Web API Basic cognition / obtain DOM Elements / Set up / modify DOM Element content and element attributes / Timer - Intermittent function / The basis of the event / Higher order function
One 、 Environment object
Environment objects refer to special objects inside functions Variable this , It represents the environment in which the current function runs
- Functions are called in different ways ,this The objects referred to are also different
- 【 Who is calling , this Who is the 】 Is a judgment this Rough rules for pointing to
- Call function directly , In fact, it's equivalent to window. function , therefore this Refer to window
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button> Click on </button>
<script>
// Environment object this He is an object
function fn() {
console.log(this)
}
// fn()
window.fn()
let btn = document.querySelector('button')
btn.addEventListener('click', function () {
console.log(typeof this)
// because btn Called this function , therefore this Point to btn
})
</script>
</body>
</html>
Two 、 Programming idea
- Use for loop
- adopt this Or the subscript finds itself or the corresponding element
Exclusive thoughts :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.pink {
background: pink;
}
</style>
</head>
<body>
<button> The first 1 individual </button><button> The first 2 individual </button><button> The first 3 individual </button><button> The first 4 individual </button><button> The first 5 individual </button>
<script>
let btns = document.querySelectorAll('button')
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function () {
// this.classList.add('pink')
// Get rid of everyone
for (let j = 0; j < btns.length; j++) {
btns[j].classList.remove('pink')
}
// Resurrect myself
this.classList.add('pink')
})
}
</script>
</body>
</html>
Exclusive thought upgrade :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.pink {
background: pink;
}
</style>
</head>
<body>
<button class="pink"> The first 1 individual </button><button> The first 2 individual </button><button> The first 3 individual </button><button> The first 4 individual </button><button> The first 5 individual </button>
<script>
let btns = document.querySelectorAll('button')
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function () {
// this.classList.add('pink')
// // Get rid of everyone
// for (let j = 0; j < btns.length; j++) {
// btns[j].classList.remove('pink')
// }
// I just need to find the one pink class , Delete
document.querySelector('.pink').classList.remove('pink')
// my
this.classList.add('pink')
})
}
</script>
</body>
</html>
Dark horse front-end column has a lot of dry goods , Focus on relearning , It's convenient ~
2022 Front end learning roadmap : Course 、 Source code 、 note , Technology stack In addition, the circuit diagram is updated in real time ! Friends who need after-school materials , You can tell me directly .
边栏推荐
- 6.28 problem solving
- Solr基础操作15
- 基于zfoo开发项目的一些规范
- JVM之栈空间
- Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
- 西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
- Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
- 【一起上水硕系列】Day 8
- What is IGMP? What is the difference between IGMP and ICMP?
- AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
猜你喜欢
随机推荐
6.28日刷题题解
Golang6 reflection
QT learning 02 GUI program example analysis
一步步教你在Edge浏览器上安装网风笔记
shell-运算符
Shell operator
AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
golang7_ TCP programming
Matlab exercises -- program control process exercise
这个简单的小功能,半年为我们产研团队省下213个小时
@Scheduled annotation pit, I stepped on it for you
Set up security groups, record domain names, and apply for SSL certificates
Solr基础操作14
Solr基础操作11
Set up enterprise NTP time server
HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
Leetcode (680) -- verifying palindrome string II
Solr基础操作10
6.28 problem solving
@Scheduled注解的坑,我替你踩了