当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Deep learning machine learning various data sets summary address
List selection JS effect with animation
Ansible learning summary (9) -- ansible loop, condition judgment, trigger, processing failure and other task control use summary
【TPM2.0原理及应用指南】 9、10、11章
USB通信协议深入理解
【网络攻防原理与技术】第5章:拒绝服务攻击
Several best practices for managing VDI
What is agile testing
简单的loading动画
99%的人都不知道|私有化部署还永久免费的即时通讯软件!
随机推荐
Understanding of 12 methods of enterprise management
Explain it in simple terms. CNN convolutional neural network
Self made dataset in pytoch for dataset rewriting
仿今日头条APP顶部点击可居中导航
calendarview日历视图组件的功能和用法
2021-06-28
Vscode three configuration files about C language
第3章业务功能开发(安全退出)
【网络攻防原理与技术】第4章:网络扫描技术
Dragging the custom style of Baidu map to the right makes the global map longitude 0 unable to be displayed normally
Cartoon | who is the first ide in the universe?
[tpm2.0 principle and Application guide] Chapter 1-3
[distributed theory] (II) distributed storage
本周小贴士#141:注意隐式转换到bool
Functions and usage of viewflipper
Pytorch中自制数据集进行Dataset重写
zdog.js火箭转向动画js特效
【网络攻防原理与技术】第7章:口令攻击技术 第8章:网络监听技术
数字化转型的主要工作
【分布式理论】(二)分布式存储