当前位置:网站首页>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年前端学习路线图:课程、源码、笔记,技术栈 另外此线路图实时更新!需要课后资料的友友们,可以直接告诉我。

边栏推荐
- Teach you step by step to install webwind notes on edge browser
- @Scheduled annotation pit, I stepped on it for you
- 我想知道今天还可以开户么?另外想问,现在在线开户安全么?
- 架构实战营模块5作业
- Analysis of define incdir command in TCL script of Modelsim
- 设置安全组、域名备案、申请ssl证书
- 机器学习:VC维的概念和用途
- Bee common configuration
- 【微信小程序】认识小程序项目的基本组成结构
- 手机开户一般哪个证券公司好?另外,手机开户安全么?
猜你喜欢

6.28日刷题题解

Halcon practical: design idea of solder joint detection

Jetpack之Room的使用,结合Flow

How about counting Berry Pie 4? What are the possible ways to play?

Siemens low code version 9.14: meet different needs

Common knowledge of ECS security settings

西门子低代码 9.14版本: 满足不同需求

Construction of module 5 of actual combat Battalion

Halcon实用:焊点检出设计思路

剑指 Offer 14- II. 剪绳子 II
随机推荐
西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
Teach you step by step to install webwind notes on edge browser
What is IGMP? What is the difference between IGMP and ICMP?
大厂试水 HPE推出Arm CPU通用服务器产品
shell-位置参数变量和预定义变量
二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
MetaQ集群安装测试
I wonder if I can open an account today? In addition, is it safe to open an account online now?
Cacti settings for spin polling
333333333333333333333333333333
Shell positional parameter variables and predefined variables
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
Unity splashimage scaling problem
数莓派 4怎么样?可能的玩法有哪些?
xutils3传集合
FPGA Development (2) -- IIC communication
Fund information disclosure
Cartoon security HIDS, EDR, NDR, XDR