当前位置:网站首页>Pro2: modify the color of div block
Pro2: modify the color of div block
2022-07-07 17:52:00 【Jing Yu】
Author URI : static Yu
🧡 brief introduction :CSDN Full stack quality Creator 、 Huawei cloud sharing expert 、 Alibaba cloud community blog expert , Founder of front-end knowledge exchange community
Community address : Front end knowledge exchange community
🧡 A variety of source code : Various benefits are full
What is? JavaScript?
The first web page is a simple static web page , To enable users to interact with web pages , Designed JavaScript Language .JavaScript Language is a language that can be used to add interactivity to web pages · Language . We usually hear right JavaScript The general description of is that this is a scripting language . A scripting language is a language that is executed in the browser , It's a program, not software . Scripting languages can be added to use html and css In the built site , It will be automatically executed when the web page is loaded , And realize the dynamic effect of various pages , It plays an important role in page interaction .
Achieve the goal
Click the button to turn the four small black squares into red , Click again to turn the red one into black
Implementation code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
button{
display: block;
margin: 0 auto;
}
#container{
width: 330px;
margin: 10px auto;
}
#container div{
width: 100px;
height: 100px;
margin: 0 5px;
background-color: black;
display: inline-block;
}
</style>
</head>
<body>
<button onclick="cli()"> Will be black div Turn red </button><br>
<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<script>
// First get div Child nodes of
var divs=document.getElementById("container").children;
// Get button
var btn=document.getElementsByTagName("button")[0];
// Click the method of the event
function cli()
{
if (btn.innerHTML === ' Will be black div Turn red '){
for (var i = 0; i < 4; i++) {
divs[i].style.backgroundColor = "red";
}
btn.innerHTML = " Turn red div black ";
}
else {
for (var i = 0; i < 4; i++) {
divs[i].style.backgroundColor = "black";
}
btn.innerHTML = " Will be black div Turn red ";
}
}
</script>
</body>
</html>
Realization effect


Implementation method
.children Get a subset of
getElementsByTagName() Method returns a collection of objects with the specified tag name , Here we get the button label directly <button>.
onclick Events occur when an element is clicked ,
innerHTML Property sets or returns the distance between the start and end labels of a table row HTML.
First get <div> Child nodes under the block , There are four <div>, And then get button label ,
Click event : Returns the HTML If it is black div Turn red , take <div> Of backgroundColor Property is set to red , On the contrary, the same is true .
边栏推荐
猜你喜欢

List selection JS effect with animation

Deep learning - make your own dataset

Please insert the disk into "U disk (H)" & unable to access the disk structure is damaged and cannot be read

【网络攻防原理与技术】第1章:绪论

运行yolo v5-5.0版本报错找不到SPPF错误,进行解决

Function and usage of numberpick

Functions and usage of viewswitch

【TPM2.0原理及应用指南】 12、13、14章

ViewSwitcher的功能和用法

Supplementary instructions to relevant rules of online competition
随机推荐
本周小贴士#134:make_unique与私有构造函数
利用七种方法对一个文件夹里面的所有图像进行图像增强实战
TaffyDB开源的JS数据库
【TPM2.0原理及应用指南】 16、17、18章
【分布式理论】(一)分布式事务
Mysql 索引命中级别分析
js拉下帷幕js特效显示层
目标管理【管理学之十四】
手机app外卖订餐个人中心页面
第1章CRM核心业务介绍
【网络攻防原理与技术】第7章:口令攻击技术 第8章:网络监听技术
阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
Based on pytorch, we use CNN to classify our own data sets
Please insert the disk into "U disk (H)" & unable to access the disk structure is damaged and cannot be read
做软件测试 掌握哪些技术才能算作 “ 测试高手 ”?
三仙归洞js小游戏源码
深入浅出图解CNN-卷积神经网络
mui侧边导航锚点定位js特效
【OKR目标管理】案例分析
【TPM2.0原理及应用指南】 9、10、11章