当前位置:网站首页>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 .
边栏推荐
- 本周小贴士#141:注意隐式转换到bool
- 面试官:页面很卡的原因分析及解决方案?【测试面试题分享】
- Function and usage of numberpick
- 本周小贴士#136:无序容器
- [distributed theory] (II) distributed storage
- 使用Stace排除故障的5种简单方法
- Introduction to OTA technology of Internet of things
- Vscode three configuration files about C language
- USB通信协议深入理解
- Explain it in simple terms. CNN convolutional neural network
猜你喜欢

Ansible learning summary (9) -- ansible loop, condition judgment, trigger, processing failure and other task control use summary

VSCode关于C语言的3个配置文件

深入浅出图解CNN-卷积神经网络

深度学习机器学习各种数据集汇总地址

《世界粮食安全和营养状况》报告发布:2021年全球饥饿人口增至8.28亿
![[OKR target management] case analysis](/img/73/d28cdf40206408be6e2ca696b8e37f.jpg)
[OKR target management] case analysis

Functions and usage of tabhost tab

自动化测试:Robot FrameWork框架大家都想知道的实用技巧

【网络攻防原理与技术】第5章:拒绝服务攻击

【信息安全法律法規】複習篇
随机推荐
viewflipper的功能和用法
yolo训练过程中批量导入requirments.txt中所需要的包
Alertdialog create dialog
Devops' operational and commercial benefits Guide
本周小贴士#140:常量:安全习语
JS pull down the curtain JS special effect display layer
【可信计算】第十次课:TPM密码资源管理(二)
第2章搭建CRM项目开发环境(搭建开发环境)
本周小贴士#136:无序容器
【网络攻防原理与技术】第3章:网络侦察技术
Use onedns to perfectly solve the optimization problem of office network
How to implement safety practice in software development stage
【分布式理论】(一)分布式事务
本周小贴士131:特殊成员函数和`= default`
【OKR目标管理】价值分析
Threshold segmentation based on RGB image and threshold adjustment by sliding
网络攻防复习篇
Functions and usage of ratingbar
serachview的功能和用法
第3章业务功能开发(实现记住账号密码)