关闭小广告【JavaScript】

news/2024/9/23 21:11:05/

在 JavaScript 中实现关闭小广告的功能,可以通过监听点击事件来隐藏广告元素。

实现效果:

代码:

<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>关闭小广告示例</title><style>.ad {width: 300px;height: 100px;background-color: lightblue;position: relative;margin: 20px;padding: 10px;border: 1px solid #000;}.close-btn {position: absolute;top: 5px;right: 5px;cursor: pointer;background-color: red;color: white;border: none;padding: 5px;}</style></head><body><div class="ad"><button class="close-btn">关闭</button><p>这是一个小广告!</p></div><script>const closeButtons = document.querySelectorAll('.close-btn');//方式一closeButtons.forEach(button => {button.addEventListener('click', function() {const ad = this.parentElement;ad.style.display = 'none'; // 隐藏广告});});// //方式二// for (let i = 0; i < closeButtons.length; i++) {// 	closeButtons[i].addEventListener('click', function() {// 		const ad = this.parentElement;// 		ad.style.display = 'none'; // 隐藏广告// 	});// }</script></body>
</html>

部分代码解析:

方式一:

javascript">const closeButtons = document.querySelectorAll('.close-btn');closeButtons.forEach(button => {button.addEventListener('click', function() {const ad = this.parentElement;ad.style.display = 'none'; // 隐藏广告});});
代码解析:

1. 选择元素:

javascript">const closeButtons = document.querySelectorAll('.close-btn');
  • 使用 document.querySelectorAll 方法选择所有带有 close-btn 类的元素,并将它们存储在 closeButtons 变量中。返回的结果是一个 NodeList(类似数组的对象)。

2. 遍历按钮:

javascript">closeButtons.forEach(button => {
  • 使用 forEach 方法遍历每个关闭按钮。

3. 添加事件监听器:

javascript">button.addEventListener('click', function() {
  • 为每个按钮添加一个点击事件监听器,当按钮被点击时,将会执行后面的函数。

4. 隐藏广告:

javascript">const ad = this.parentElement;
ad.style.display = 'none'; // 隐藏广告
  • 在点击事件处理函数中,this 代表被点击的按钮。this.parentElement 获取按钮的父元素(假设这是广告的容器),然后将其 display 样式设置为 'none',这会隐藏这个广告元素。

方式二:

javascript">const closeButtons = document.querySelectorAll('.close-btn');for (let i = 0; i < closeButtons.length; i++) {closeButtons[i].addEventListener('click', function() {const ad = this.parentElement;ad.style.display = 'none'; // 隐藏广告});
}
代码解析: 
  • 使用 for 循环,从 0 开始,直到 closeButtons.length,逐个访问每个关闭按钮。
  • 使用 closeButtons[i] 来获取当前按钮,并添加点击事件监听器。
  • this.parentElement 是用于获取当前 DOM 元素的父元素。在事件处理函数中,this 代表触发事件的元素(在你的例子中是关闭按钮),而 parentElement 属性则返回该元素的父节点。

http://www.ppmy.cn/news/1529496.html

相关文章

更新了,图片美化工具迎来两大功能

最近给图片套壳美化工具更新了两个重要的功能&#xff0c;简而言之就是&#xff1a; 直接操作图片的放大缩小快速截图后上传图片&#xff08;通过浏览器的 getDisplayMedia 接口&#xff09; 第一个功能是为了方便操作&#xff0c;之前是在左侧通过滑动区块来调节图片区域的显示…

【LLM论文日更】| 俄罗斯套娃嵌入模型

论文&#xff1a;https://proceedings.neurips.cc/paper_files/paper/2022/file/c32319f4868da7613d78af9993100e42-Paper-Conference.pdf代码&#xff1a;GitHub - RAIVNLab/MRL: Code repository for the paper - "Matryoshka Representation Learning"机构&#x…

MATLAB数据文件读写:1.格式化读写文件

格式化读写文件 matlab提供了对数据文件建立、打开、读取、写入、关闭等操作的函数。 数据文件可以分为两类&#xff1a; 文本文件&#xff1a;以ASCII码形式存储的文本文件&#xff1b;编码基于字符定长&#xff0c;译码相对容易二进制文件&#xff1a;以二进制形式存储的文…

Allow anonymous access to my Azure OpenAI chat bot

题意&#xff1a;允许匿名访问我的 Azure OpenAI 聊天机器人 问题背景&#xff1a; I have an Azure OpenAI chat bot using my own data (I configured an OpenAI resource and chose Deploy as Web App) . Members of my domain can access it by logging in. Now I want it…

C++学习笔记(32)

三十四、双链表 示例&#xff1a; #include <iostream> using namespace std; typedef int ElemType; // 自定义链表的数据元素为整数。 struct LNode // 双链表的结点。 { ElemType data; // 存放结点的数据元素。 struct LNode* prior,*next; // 前驱和后继结点的指针。…

Nginx实用篇:实现负载均衡、限流与动静分离

Nginx实用篇&#xff1a;实现负载均衡、限流与动静分离 | 原创作者/编辑&#xff1a;凯哥Java | 分类&#xff1a;Nginx学习系列教程 Nginx 作为一款高性能的 HTTP 服务器及反向代理解决方案&#xff0c;在互联网架构中扮演着至关重要的角色。它…

计算机网络33——文件系统

1、chmod 2、chown 需要有root权限 3、link 链接 4、unlink 创建临时文件&#xff0c;用于非正常退出 5、vi vi可以打开文件夹 ../是向外一个文件夹 6、ls ls 可以加很多路径&#xff0c;路径可以是文件夹&#xff0c;也可以是文件 ---------------------------------…

vue选项式写法项目案例(购物车)

一、初始化项目结构 1.初始化vite项目 npm create vite cd vite-project npm install 2.清理项目结构 清空App.vue 删除components目录下的HelloWorld.vue组件 3.为组件的样式启用sacc或less组件 npm i sass4.初始化index.css全局样式 :root{font-size:12px } 二、封装…