javascript - 单独的点击,相同的按钮,功能然后恢复正常
问题描述
我可以单击按钮并对其进行更改,但我希望能够再次单击它并使其恢复正常,最好使用 Vanilla JavaScript 或 JavaScript。
我已经包含了我的 HTML、CSS 和 JavaScript 代码。看了很多论坛,大部分问题都是关于一键运行多个功能的;但是,我希望能够一次又一次地运行单独的功能。或者有没有办法让第二次点击自动让它恢复正常而不编写单独的函数?
function thermometer(){
var a;
a=document.getElementById('span1');
a.innerHTML = `<i class="fas fa-thermometer-empty"></i>`;
setTimeout(function () {
a.innerHTML = `<i class="fas fa-thermometer-quarter"></i>`;
}, 1000);
setTimeout(function(){
a.innerHTML = `<i class="fas fa-thermometer-half"></i>`;
}, 2000)
setTimeout(function(){
a.innerHTML = `<i class="fas fa-thermometer-three-quarters"></i>`;
}, 3000)
setTimeout(function(){
a.innerHTML = `<i class="fas fa-thermometer-full"></i>`;
}, 4000)
}
thermometer();
setInterval(thermometer, 5000);
function myFunction(){
b=document.getElementById('jo');
b.innerHTML = `<i class="fas fa-meh-rolling-eyes"></i>`;
}
#span1{
font-size: 80px;
color: hotpink;
padding-left: 20px;
}
#span1:hover {
color:rgb(219, 33, 0);
}
.todo-button{
font-size: 38px;
width: 120px;
height: 60px;
color: white;
background: lawngreen;
border: none;
border-radius: 6px;
}
.fa-grin-tongue:hover{
color: yellow;
}
h1{
font-family: 'Paytone One', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
padding-bottom: 20px;
font-size: 45px;
color:hotpink;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Caveat:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.1/css/all.css" integrity="sha384-vp86vTRFVJgpjF9jiIGPEEqYqlDwgyBgEF109VFjmqGmIY/Y4HV4d3Gp2irVfcrp" crossorigin="anonymous">
<title>Todo List</title>
</head>
<body>
<header>
<h1 id="h1t">Thermo-Listing<span id="span1"></span></h1>
<form class="todo-input"></form>
<!--Button Information Below-->
<button onclick="myFunction();" class="todo-button" type="submit" id="jo"><i class="fas fa-grin-tongue"></i></button>
</header>
<div class="todo-container">
<ul class="todo-list">
</ul>
</div>
<script src="script.js"></script>
<script src="https://kit.fontawesome.com/e4dab53a8c.js" crossorigin="anonymous"></script>
</body>
</html>
解决方案
我正在引入一个toggle
变量,该变量会随着您的函数内的每次点击而增加。根据模数% 2
,我们可以交替切换来显示您不同的表情符号。
let toggle = 0;
const rolling = '<i class="fas fa-meh-rolling-eyes"></i>';
const tongue = '<i class="fas fa-grin-tongue"></i>';
function thermometer(){
var a;
a=document.getElementById('span1');
a.innerHTML = `<i class="fas fa-thermometer-empty"></i>`;
setTimeout(function () {
a.innerHTML = `<i class="fas fa-thermometer-quarter"></i>`;
}, 1000);
setTimeout(function(){
a.innerHTML = `<i class="fas fa-thermometer-half"></i>`;
}, 2000)
setTimeout(function(){
a.innerHTML = `<i class="fas fa-thermometer-three-quarters"></i>`;
}, 3000)
setTimeout(function(){
a.innerHTML = `<i class="fas fa-thermometer-full"></i>`;
}, 4000)
}
thermometer();
setInterval(thermometer, 5000);
function myFunction(){
b=document.getElementById('jo');
b.innerHTML = (++toggle % 2) ? rolling : tongue;
}
#span1{
font-size: 80px;
color: hotpink;
padding-left: 20px;
}
#span1:hover {
color:rgb(219, 33, 0);
}
.todo-button{
font-size: 38px;
width: 120px;
height: 60px;
color: white;
background: lawngreen;
border: none;
border-radius: 6px;
}
.fa-grin-tongue:hover{
color: yellow;
}
h1{
font-family: 'Paytone One', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
padding-bottom: 20px;
font-size: 45px;
color:hotpink;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Caveat:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.1/css/all.css" integrity="sha384-vp86vTRFVJgpjF9jiIGPEEqYqlDwgyBgEF109VFjmqGmIY/Y4HV4d3Gp2irVfcrp" crossorigin="anonymous">
<title>Todo List</title>
</head>
<body>
<header>
<h1 id="h1t">Thermo-Listing<span id="span1"></span></h1>
<form class="todo-input"></form>
<!--Button Information Below-->
<button onclick="myFunction();" class="todo-button" type="submit" id="jo"><i class="fas fa-grin-tongue"></i></button>
</header>
<div class="todo-container">
<ul class="todo-list">
</ul>
</div>
<script src="script.js"></script>
<script src="https://kit.fontawesome.com/e4dab53a8c.js" crossorigin="anonymous"></script>
</body>
</html>
推荐阅读
- android - 无法解析 activitycompat 和 appcompatactivity
- python - 为什么我得到一个错误,但我仍然得到正确的输出,有没有人有解决方案?
- java - Coinbase 分页返回 {"errors":[{"id":"not_found","message":"Not found"}]}
- python - 将类对象列表保存到 JSON 文件中,以便以后读取
- google-sheets - 在谷歌表格查询中使用参考单元格作为列的一部分
- php - 如果文件夹不包含使用 .htaccess 的 index.php/.html,如何显示内容?
- rust - 如何在 Rust 中使字符(char)大写
- c# - 将查询结果从 Powershell 调用到 C#
- c# - 我正在使用 MVC 并尝试从 Query 将值传递给我的视图
- java - Junit测试期间线程“main”java.lang.IllegalAccessError中的异常