javascript - 如何为活动按钮之前的所有按钮设置相同的颜色,并为活动按钮旁边的所有按钮设置相同的颜色?
问题描述
我在设置按钮样式时遇到问题。我需要的是,活动按钮和活动之前的所有按钮都具有相同的颜色,而活动按钮旁边的所有按钮都具有其他颜色。这可以在javascript中实现吗?
代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
}
.active, .btn:hover {
background-color: #666;
color: white;
}
.mystyle{
background-color:gold;
}
</style>
</head>
<body>
<h1>Active Button</h1>
<p>Highlight the active/current (pressed) button.</p>
<div id="myDIV">
<button class="btn">1</button>
<button class="btn">2</button>
<button class="btn">3</button>
<button class="btn">4</button>
<button class="btn">5</button>
</div>
<script>
// Add active class to the current button (highlight it)
var header = document.getElementById("myDIV");
var btns = header.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
if (current.length > 0) {
current[0].className = current[0].className.replace(" active", "");
}
this.className += " active";
this.previousSibling.className += " mystyle";
});
}
</script>
</body>
</html>
解决方案
我希望它对你有所帮助。
function removeClasses() {
for(var i=1;i<=5;i++) {
$('#'+i+'').removeClass('active');
$('#'+i+'').removeClass('mystyle');
$('#'+i+'').removeClass('next');
}
}
function activeButton(id) {
id = parseInt(id);
removeClasses();
$('#'+id+'').addClass('active');
for(var j=1;j<=(id-1);j++) $('#'+j+'').addClass('mystyle');
for(var j=(id+1);j<=5;j++) $('#'+j+'').addClass('next');
}
.btn {
border: none;
outline: none;
padding: 10px 16px;
background-color: #f1f1f1;
cursor: pointer;
font-size: 18px;
}
.active, .btn:hover {
background-color: #666;
color: white;
}
.mystyle{
background-color:gold;
}
.next {
background-color:green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Active Button</h1>
<p>Highlight the active/current (pressed) button.</p>
<div id="myDIV">
<button id="1" onclick="activeButton(this.id)" class="btn">1</button>
<button id="2" onclick="activeButton(this.id)" class="btn">2</button>
<button id="3" onclick="activeButton(this.id)" class="btn">3</button>
<button id="4" onclick="activeButton(this.id)" class="btn">4</button>
<button id="5" onclick="activeButton(this.id)" class="btn">5</button>
</div>
</body>
</html>
推荐阅读
- java - 在 Fragment 中使用 DataBinding 与不同的相关 Activity 包
- javascript - 将 P5js AudioIn 获取到 MediaStream 对象
- sql-server - SQL Server 将 XML 解析为表 - 具有相同名称的多个节点
- python - 按列值连接两个数据框
- python - 资源“00000003-0000-0000-c000-000000000000”上不存在的范围“Mail.ReadBasic.All”
- java - Axon Framework - 我如何回滚 Saga 进程
- apache-nifi - NiFi“架构注册表”无效
- graphql - Apollo graphql:makeExecutableSchema 和游乐场
- c# - 有时 IOrganizationService 对象为空
- javascript - 如何对 observable 上的数据数组进行排序?