当前位置:网站首页>Web APIs 环境对象 丨黑马程序员
Web APIs 环境对象 丨黑马程序员
2022-06-29 23:50:00 【黑马程序员官方】
前期更新笔记内容:Web API 基本认知 / 获取DOM元素 / 设置/修改DOM元素内容和元素属性 / 定时器-间歇函数 /事件基础 / 高阶函数
一、 环境对象
环境对象指的是函数内部特殊的变量 this ,它代表着当前函数运行时所处的环境
- 函数的调用方式不同,this 指代的对象也不同
- 【谁调用, this 就是谁】 是判断 this 指向的粗略规则
- 直接调用函数,其实相当于是 window.函数,所以 this 指代 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>点击</button>
<script>
// 环境对象 this 他就是个对象
function fn() {
console.log(this)
}
// fn()
window.fn()
let btn = document.querySelector('button')
btn.addEventListener('click', function () {
console.log(typeof this)
// 因为btn 调用了这个函数,所以 this 指向btn
})
</script>
</body>
</html>二、编程思想
- 使用for循环
- 通过this或者下标找到自己或者对应的元素
排他思想:
<!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>第1个</button><button>第2个</button><button>第3个</button><button>第4个</button><button>第5个</button>
<script>
let btns = document.querySelectorAll('button')
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function () {
// this.classList.add('pink')
// 干掉所有人
for (let j = 0; j < btns.length; j++) {
btns[j].classList.remove('pink')
}
// 复活我自己
this.classList.add('pink')
})
}
</script>
</body>
</html>排他思想升级:
<!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">第1个</button><button>第2个</button><button>第3个</button><button>第4个</button><button>第5个</button>
<script>
let btns = document.querySelectorAll('button')
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function () {
// this.classList.add('pink')
// // 干掉所有人
// for (let j = 0; j < btns.length; j++) {
// btns[j].classList.remove('pink')
// }
// 我只需要找出那个唯一的 pink类,删除
document.querySelector('.pink').classList.remove('pink')
// 我的
this.classList.add('pink')
})
}
</script>
</body>
</html>黑马前端专栏干货多多,关注再学,好方便~
2022年前端学习路线图:课程、源码、笔记,技术栈 另外此线路图实时更新!需要课后资料的友友们,可以直接告诉我。

边栏推荐
猜你喜欢

FPGA开发(1)——串口通信

Construction of module 5 of actual combat Battalion

6.29 problem solving

机器学习:VC维的概念和用途

云和恩墨盖国强,识别它、抓住它,在国产数据库沸腾以前

Leetcode (76) -- Minimum Covering substring

【一起上水硕系列】Day 8

The role of VMware virtual machine

剑指 Offer 15. 二进制中1的个数

Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query
随机推荐
HPE launched ARM CPU general server product
Siemens low code version 9.14: meet different needs
Simple understanding of B tree and b+ tree
Golang泛型的巧妙应用,防止变量空指针错误,防止结构体字段空指针错误
【微信小程序】认识小程序项目的基本组成结构
6.28 problem solving
[Shangshui Shuo series] day 8
Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
Head on Amway! Good looking and practical motor SolidWorks model material see here
Sword finger offer 14- I. cut rope
6.29日刷题题解
I wonder if I can open an account today? In addition, is it safe to open an account online now?
Construction of module 5 of actual combat Battalion
modelsim的TCL脚本的define incdir命令解析
RRDtool 画MRTG Log数据
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
What is IGMP? What is the difference between IGMP and ICMP?
西门子低代码 9.14版本: 满足不同需求
为什么 JSX 语法这么香?
Set up enterprise NTP time server