<ul class="nav">
<li>我的首页</li>
<li>产品介绍</li>
<li>联系方式</li>
</ul>
<script>
const result = document.querySelectorAll('li');
for(let i = 0; i < result.length; i++){
console.log(result[i]);
}
</script>
<!--
根据 id 获取一个元素
document.getElementById('nav');
根据 类名 获取页面内所有指定类名元素
document.getElementsByClassName('biu);
根据标签获取页面内指定标签的所有元素
document.getElementsByTagName('div');
-->
<!--两种获取标签内容的方式-->
<ul class="nav">
<li>我的首页</li>
<li>产品介绍</li>
<li>联系方式</li>
</ul>
<script>
const info = document.querySelector('ul');
/*
innerText 获得的是纯文本,不能解析标签;innerHTML 不但可以获得文本,还能解析标签(不包括当前元素的标签,可以显示子标签)
我的首页、我的首页
我的首页、<li>我的首页</li>
产品介绍、<li>产品介绍</li>
联系方式、<li>联系方式</li>
*/
console.log(info.innerText);
console.log(info.innerHTML);
</script>
<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>年会抽奖</title>
<style>
.wrapper {
width: 840px;
height: 420px;
background: url(bg01.jpg) no-repeat center / cover;
padding: 100px 250px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="wrapper">
<strong>传智教育年会抽奖</strong>
<h1>一等奖:<span id="one">???</span></h1>
<h3>二等奖:<span id="two">???</span></h3>
<h5>三等奖:<span id="three">???</span></h5>
</div>
<script>
// 实现思路: 准备一个数组存储所有抽奖参与人,利用随机数选人,赋值给我们对应的标签元素,抽出一个人就删掉一个人
const candidate = ['周杰伦', '刘德华', '周星驰', 'Pink老师', '张学友'];
let idx = Math.floor(Math.random() * candidate.length);
const one = document.querySelector('#one'); // 尽量精确到要修改的元素的标签
one.innerText = candidate[idx];
candidate.splice(idx, 1);
idx = Math.floor(Math.random() * candidate.length);
const two = document.querySelector('#two');
two.innerText = candidate[idx];
candidate.splice(idx, 1);
idx = Math.floor(Math.random() * candidate.length);
const three = document.querySelector('#three');
three.innerText = candidate[idx];
candidate.splice(idx, 1);
</script>
</body>
</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">
</head>
<body>
<img src="1.webp" title="我不是帅哥" />
<script>
const photo = document.querySelector('img');
const idx = Math.floor(Math.random() * 6) + 1;
photo.src = idx + '.webp';
</script>
</body>
</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">
<style>
body {
/*no-repeat 不重复、top 垂直顶部、center 水平中间、/cover 等比例缩放整个区域*/
background: url(desktop_1.jpg) no-repeat top center/cover;
}
</style>
</head>
<!--如果使用 " 会被浏览器解析成双引号-->
<body style="background-image: url(desktop_1.jpg)">
<script>
const ele = document.querySelector('body');
const idx = Math.floor(Math.random() * 10) + 1;
ele.style.backgroundImage = `url(desktop_${idx}.jpg)`;
</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>轮播图点击切换</title>
<style>
* {
box-sizing: border-box;
}
.slider {
width: 560px;
height: 400px;
overflow: hidden;
}
.slider-wrapper {
width: 100%;
height: 320px;
}
.slider-wrapper img {
width: 100%;
height: 100%;
display: block;
}
.slider-footer {
height: 80px;
background-color: rgb(100, 67, 68);
padding: 12px 12px 0 12px;
position: relative;
}
.slider-footer .toggle {
position: absolute;
right: 0;
top: 12px;
display: flex;
}
.slider-footer .toggle button {
margin-right: 12px;
width: 28px;
height: 28px;
appearance: none;
border: none;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 4px;
cursor: pointer;
}
.slider-footer .toggle button:hover {
background: rgba(255, 255, 255, 0.2);
}
.slider-footer p {
margin: 0;
color: #fff;
font-size: 18px;
margin-bottom: 10px;
}
.slider-indicator {
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
}
.slider-indicator li {
width: 8px;
height: 8px;
margin: 4px;
border-radius: 50%;
background: #fff;
opacity: 0.4;
cursor: pointer;
}
.slider-indicator li.active {
width: 12px;
height: 12px;
opacity: 1;
}
</style>
</head>
<body>
<div class="slider">
<div class="slider-wrapper">
<img src=".slider01.jpg" alt="" />
</div>
<div class="slider-footer">
<p>对人类来说会不会太超前了?</p>
<ul class="slider-indicator">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="toggle">
<button class="prev"><</button>
<button class="next">></button>
</div>
</div>
</div>
<script>
// 1. 初始数据
const sliderData = [
{ url: 'slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: 'slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: 'slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: 'slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: 'slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: 'slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: 'slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: 'slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
const random = parseInt(Math.random() * sliderData.length);
const img = document.querySelector('.slider-wrapper img');
img.src = sliderData[random].url;
const p = document.querySelector('.slider-footer p');
p.innerHTML = sliderData[random].title;
const footer = document.querySelector('.slider-footer');
footer.color = sliderData[random].color;
// 清除之前的小点
document.querySelector(`.slider-indicator .active`).classList.remove('active');
const li = document.querySelector(`.slider-indicator li:nth-child(${random + 1})`)
li.classList.add('active')
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea name="" id="" cols="30" rows="10">
用户注册协议
欢迎注册成为京东用户!在您注册过程中,您需要完成我们的注册流程并通过点击同意的形式在线签署以下协议,请您务必仔细阅读、充分理解协议中的条款内容后再点击同意(尤其是以粗体或下划线标识的条款,因为这些条款可能会明确您应履行的义务或对您的权利有所限制)。
【请您注意】如果您不同意以下协议全部或任何条款约定,请您停止注册。您停止注册后将仅可以浏览我们的商品信息但无法享受我们的产品或服务。如您按照注册流程提示填写信息,阅读并点击同意上述协议且完成全部注册流程后,即表示您已充分阅读、理解并接受协议的全部内容,并表明您同意我们可以依据协议内容来处理您的个人信息,并同意我们将您的订单信息共享给为完成此订单所必须的第三方合作方(详情查看
</textarea>
<br>
<button class="btn">我已经阅读用户协议(5)</button>
<script>
const btn = document.querySelector('.btn');
let remainder = 5;
// 第二个参数代表定时器的执行频率,1000ms 代表每一秒执行一次
let n = setInterval(function(){
remainder--;
btn.innerHTML = `我已经阅读用户协议(${remainder})`;
if(remainder === 0){
clearInterval(n);
btn.disabled = false; // 取消按钮的禁用状态
btn.innerHTML = '同意';
}
}, 1000);
</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>轮播图点击切换</title>
<style>
* {
box-sizing: border-box;
}
.slider {
width: 560px;
height: 400px;
overflow: hidden;
}
.slider-wrapper {
width: 100%;
height: 320px;
}
.slider-wrapper img {
width: 100%;
height: 100%;
display: block;
}
.slider-footer {
height: 80px;
background-color: rgb(100, 67, 68);
padding: 12px 12px 0 12px;
position: relative;
}
.slider-footer .toggle {
position: absolute;
right: 0;
top: 12px;
display: flex;
}
.slider-footer .toggle button {
margin-right: 12px;
width: 28px;
height: 28px;
appearance: none;
border: none;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 4px;
cursor: pointer;
}
.slider-footer .toggle button:hover {
background: rgba(255, 255, 255, 0.2);
}
.slider-footer p {
margin: 0;
color: #fff;
font-size: 18px;
margin-bottom: 10px;
}
.slider-indicator {
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
}
.slider-indicator li {
width: 8px;
height: 8px;
margin: 4px;
border-radius: 50%;
background: #fff;
opacity: 0.4;
cursor: pointer;
}
.slider-indicator li.active {
width: 12px;
height: 12px;
opacity: 1;
}
</style>
</head>
<body>
<div class="slider">
<div class="slider-wrapper">
<img src=".slider01.jpg" alt="" />
</div>
<div class="slider-footer">
<p>对人类来说会不会太超前了?</p>
<ul class="slider-indicator">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="toggle">
<button class="prev"><</button>
<button class="next">></button>
</div>
</div>
</div>
<script>
// 1. 初始数据
const sliderData = [
{ url: 'slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: 'slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: 'slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: 'slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: 'slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: 'slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: 'slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: 'slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
let idx = 0;
const img = document.querySelector('.slider-wrapper img');
const p = document.querySelector('.slider-footer p');
setInterval(function(){
idx++;
idx %= 8;
img.src = sliderData[idx].url;
p.innerHTML = sliderData[idx].title;
document.querySelector('.slider-indicator .active').classList.remove('active');
document.querySelector(`.slider-indicator li:nth-child(${idx + 1})`).classList.add('active');
}, 1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.father {
position: relative;
width: 1000px;
height: 200px;
background-color: pink;
margin: 100px auto;
text-align: center;
font-size: 50px;
line-height: 200px;
font-weight: 700;
}
.son {
position: absolute;
right: 20px;
top: 10px;
width: 20px;
height: 20px;
background-color: skyblue;
text-align: center;
line-height: 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="father">
"我是广告"
<div class="son">X</div>
</div>
<script>
const father = document.querySelector('.father');
const son = document.querySelector('.son');
// 监听子盒子
son.addEventListener('click', function(){
// 关闭父盒子
father.style.display = 'none';
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
h2 {
text-align: center;
}
.box {
width: 600px;
margin: 50px auto;
display: flex;
font-size: 25px;
line-height: 40px;
}
.qs {
width: 450px;
height: 40px;
color: red;
}
.btns {
text-align: center;
}
.btns button {
width: 120px;
height: 35px;
margin: 0 50px;
}
</style>
</head>
<body>
<h2>随机点名</h2>
<div class="box">
<span>名字是:</span>
<div class="qs">这里显示姓名</div>
</div>
<div class="btns">
<button class="start">开始</button>
<button class="end">结束</button>
</div>
<script>
// 数据数组
const arr = ['马超', '黄忠', '赵云', '关羽', '张飞']
// 获取到我们的两个按钮
const start = document.querySelector('.start');
const end = document.querySelector('.end');
const qs = document.querySelector('.qs');
// 定时任务 + 随机选人
let timeId = 0; // 定时器 id 要设置成全局的,因为我们两个按钮的监听事件都要根据它处理
let idx = 0;
start.addEventListener('click', function(){
timeId = setInterval(function(){
idx = Math.floor(Math.random() * arr.length);
qs.innerHTML = arr[idx];
}, 10);
// 数组中剩一个元素就不用再取值,禁用两个按钮
if(arr.length === 1){
start.disabled = end.disabled = true;
// start.style.display = end.style.display = 'none'; 代表不显示元素
}
}, 10);
end.addEventListener('click', function(){
clearInterval(timeId);
arr.splice(idx, 1);
console.log(arr);
});
</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>轮播图点击切换</title>
<style>
* {
box-sizing: border-box;
}
.slider {
width: 560px;
height: 400px;
overflow: hidden;
}
.slider-wrapper {
width: 100%;
height: 320px;
}
.slider-wrapper img {
width: 100%;
height: 100%;
display: block;
}
.slider-footer {
height: 80px;
background-color: rgb(100, 67, 68);
padding: 12px 12px 0 12px;
position: relative;
}
.slider-footer .toggle {
position: absolute;
right: 0;
top: 12px;
display: flex;
}
.slider-footer .toggle button {
margin-right: 12px;
width: 28px;
height: 28px;
appearance: none;
border: none;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border-radius: 4px;
cursor: pointer;
}
.slider-footer .toggle button:hover {
background: rgba(255, 255, 255, 0.2);
}
.slider-footer p {
margin: 0;
color: #fff;
font-size: 18px;
margin-bottom: 10px;
}
.slider-indicator {
margin: 0;
padding: 0;
list-style: none;
display: flex;
align-items: center;
}
.slider-indicator li {
width: 8px;
height: 8px;
margin: 4px;
border-radius: 50%;
background: #fff;
opacity: 0.4;
cursor: pointer;
}
.slider-indicator li.active {
width: 12px;
height: 12px;
opacity: 1;
}
</style>
</head>
<body>
<div class="slider">
<div class="slider-wrapper">
<img src="slider01.jpg" alt="" />
</div>
<div class="slider-footer">
<p>对人类来说会不会太超前了?</p>
<ul class="slider-indicator">
<li class="active"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="toggle">
<button class="prev"><</button>
<button class="next">></button>
</div>
</div>
</div>
<script>
// 1. 初始数据
const sliderData = [
{ url: 'slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: 'slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: 'slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: 'slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: 'slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: 'slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: 'slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: 'slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
// 设置图片索引
let idx = 0;
// 拿到我们对应的 button
const prev = document.querySelector('.prev');
const next = document.querySelector('.next');
const img = document.querySelector('.slider-wrapper img');
const p = document.querySelector('.slider-footer p');
// 监听点击事件
prev.addEventListener('click', function(){
// 更新索引
idx--;
idx = idx < 0 ? sliderData.length - 1 : idx;
toggle();
});
next.addEventListener('click', function(){
// 更新索引
idx++;
idx = idx === sliderData.length ? 0 : idx;
toggle();
});
// 复用渲染函数
function toggle(){
// 更新图片地址
img.src = sliderData[idx].url;
// 更新文字内容
p.innerHTML = sliderData[idx].title;
// 更新小圆点
document.querySelector('.slider-indicator .active').classList.remove('active');
document.querySelector(`.slider-indicator li:nth-child(${idx + 1})`).classList.add('active');
}
// 鼠标经过暂停定时器,鼠标离开开启定时器 >> 效果就是不去点击切换按钮的时候,轮播图是自动更换的
const slider = document.querySelector('.slider');
// 定时器
let id = setInterval(function(){
next.click();
}, 1000);
// 监听鼠标移入移出事件
slider.addEventListener('mouseenter', function(){
clearInterval(id);
});
slider.addEventListener('mouseleave', function(){
if(id){
clearInterval(id);
}
id = setInterval(function(){
next.click();
}, 1000);
});
</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>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul {
list-style: none;
}
.mi {
position: relative;
width: 223px;
margin: 100px auto;
}
.mi input {
width: 223px;
height: 48px;
padding: 0 10px;
font-size: 14px;
line-height: 48px;
border: 1px solid #e0e0e0;
outline: none;
}
.mi .search {
border: 1px solid #ff6700;
}
.result-list {
position: absolute;
left: 0;
top: 48px;
width: 223px;
border: 1px solid #ff6700;
border-top: 0;
background: #fff;
}
.result-list a {
display: block;
padding: 6px 15px;
font-size: 12px;
color: #424242;
text-decoration: none;
}
.result-list a:hover {
background-color: #eee;
}
</style>
</head>
<body>
<div class="mi">
<input type="search" placeholder="小米笔记本">
<ul class="result-list">
<li><a href="#">全部商品</a></li>
<li><a href="#">小米11</a></li>
<li><a href="#">小米10S</a></li>
<li><a href="#">小米笔记本</a></li>
<li><a href="#">小米手机</a></li>
<li><a href="#">黑鲨4</a></li>
<li><a href="#">空调</a></li>
</ul>
</div>
<script>
// 注意拿到表单不应该用 .mi 而是通过 [type=search] 来完成
const input = document.querySelector('[type=search]');
const list = document.querySelector('.result-list');
input.addEventListener('focus', function(){
list.style.display = 'block';
input.classList.add('search'); // 因为对于列表部分是有边框颜色的
});
input.addEventListener('blur', function(){
list.style.display = 'none';
input.classList.remove('search');
});
</script>
</body>
</html>
<!--统计文字字数-->
<input class="input" type="text"/>
<script>
const input = document.querySelector('.input');
let len = 0;
input.addEventListener('input', function(){
len = input.value.length;
console.log('输入内容的长度为: ' + len);
});
</script>
<!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>评论回车发布</title>
<style>
.wrapper {
min-width: 400px;
max-width: 800px;
display: flex;
justify-content: flex-end;
}
.avatar {
width: 48px;
height: 48px;
border-radius: 50%;
overflow: hidden;
background: url(./images/avatar.jpg) no-repeat center / cover;
margin-right: 20px;
}
.wrapper textarea {
outline: none;
border-color: transparent;
resize: none;
background: #f5f5f5;
border-radius: 4px;
flex: 1;
padding: 10px;
transition: all 0.5s;
height: 30px;
}
.wrapper textarea:focus {
border-color: #e4e4e4;
background: #fff;
height: 50px;
}
.wrapper button {
background: #00aeec;
color: #fff;
border: none;
border-radius: 4px;
margin-left: 10px;
width: 70px;
cursor: pointer;
}
.wrapper .total {
margin-right: 80px;
color: #999;
margin-top: 5px;
opacity: 0;
transition: all 0.5s;
}
.list {
min-width: 400px;
max-width: 800px;
display: flex;
}
.list .item {
width: 100%;
display: flex;
}
.list .item .info {
flex: 1;
border-bottom: 1px dashed #e4e4e4;
padding-bottom: 10px;
}
.list .item p {
margin: 0;
}
.list .item .name {
color: #FB7299;
font-size: 14px;
font-weight: bold;
}
.list .item .text {
color: #333;
padding: 10px 0;
}
.list .item .time {
color: #999;
font-size: 12px;
}
</style>
</head>
<body>
<div class="wrapper">
<i class="avatar"></i>
<textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
<button>发布</button>
</div>
<div class="wrapper">
<span class="total">0/200字</span>
</div>
<div class="list">
<div class="item" style="display: none;">
<i class="avatar"></i>
<div class="info">
<p class="name">清风徐来</p>
<p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
<p class="time">2022-10-10 20:29:21</p>
</div>
</div>
</div>
<script>
// 获取到文本框中的内容,以及下面要显示字数的部分
const tx = document.querySelector('#tx');
const total = document.querySelector('.total');
// 鼠标点击输入框,显示字数,点击其他区域,不显示字数
tx.addEventListener('focus', function(){
total.style.opacity = 1; // 通过 opacity 指定透明度来设置是否显示
});
tx.addEventListener('blur', function(){
total.style.opacity = 0;
});
tx.addEventListener('input', function(){
let len = tx.value.length;
if(len > 200){
alert('评论字数不能超过两百字');
}
total.innerHTML = `${len}/200字`;
});
</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>tab栏切换</title>
<style>
* {
margin: 0;
padding: 0;
}
.tab {
width: 590px;
height: 340px;
margin: 20px;
border: 1px solid #e4e4e4;
}
.tab-nav {
width: 100%;
height: 60px;
line-height: 60px;
display: flex;
justify-content: space-between;
}
.tab-nav h3 {
font-size: 24px;
font-weight: normal;
margin-left: 20px;
}
.tab-nav ul {
list-style: none;
display: flex;
justify-content: flex-end;
}
.tab-nav ul li {
margin: 0 20px;
font-size: 14px;
}
.tab-nav ul li a {
text-decoration: none;
border-bottom: 2px solid transparent;
color: #333;
}
.tab-nav ul li a.active {
border-color: #e1251b;
color: #e1251b;
}
.tab-content {
padding: 0 16px;
}
.tab-content .item {
display: none;
}
.tab-content .item.active {
display: block;
}
</style>
</head>
<body>
<div class="tab">
<div class="tab-nav">
<h3>每日特价</h3>
<ul>
<li><a class="active" href="javascript:;">精选</a></li>
<li><a href="javascript:;">美食</a></li>
<li><a href="javascript:;">百货</a></li>
<li><a href="javascript:;">个护</a></li>
<li><a href="javascript:;">预告</a></li>
</ul>
</div>
<div class="tab-content">
<div class="item active"><img src="tab00.png" alt="" /></div>
<div class="item"><img src="tab01.png" alt="" /></div>
<div class="item"><img src="tab02.png" alt="" /></div>
<div class="item"><img src="tab03.png" alt="" /></div>
<div class="item"><img src="tab04.png" alt="" /></div>
</div>
<script>
// 鼠标移入切换图片
const as = document.querySelectorAll('.tab-nav a');
// 要给每一个列表和对应图片建立关系
for(let i = 0; i < as.length; i++){
as[i].addEventListener('mouseenter', function(){
//清除已有 active 的标签
document.querySelector('.tab-nav .active').classList.remove('active');
this.classList.add('active');
document.querySelector('.tab-content .active').classList.remove('active');
document.querySelector(`.tab-content .item:nth-child(${i + 1})`).classList.add('active');
});
}
</script>
</div>
</body>
</html>
标签:const,入门,第三节,width,color,JS,height,slider,document
From: https://www.cnblogs.com/20200109-zwh/p/17642593.html