html - 我如何对单独的单独应用按钮列表(在检查一个按钮时)效果?
问题描述
我想要做的是让用户在整个评分范围内单击任何数字(每个数字都作为单选按钮),并且只在该数字周围设置设计的背景颜色。但是,正如我在下面演示的那样,例如,当用户单击 #1 时,它会更改刻度中每个单选按钮的背景颜色。我如何将它应用于每个人?
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body{
margin: 0px;
padding: 0px;
background-color: bisque;
overflow: hidden;
}
.head{
text-align: center;
transform: translate(20px, 150px);
}
.rating{
transform: translate(320px, 200px);
}
.rating label{
cursor: pointer;
margin-right: 30px;
border: 1px solid #000;
border-radius: 25px;
padding: 5px 10px;
background-color: white;
}
.rating input{
display: none;
}
.labels{
display: flex;
list-style: none;
}
input#rate1:checked ~ label{
background-color: blue;
}
.bottom{
transform: translate(300px, 150px);
}
</style>
</head>
<body>
<div class="head">
<h1>On a Scale From 0-10, How Are You Feeling?</h1>
</div>
<div class="labels">
<ul>
<li>Rock Bottom</li>
<li>Terrible</li>
</ul>
</div>
<div class="rating">
<input type="radio" name="rating" id="rate0"><label for="rate0"
>0</label>
<input type="radio" name="rating" id="rate1"><label for="rate1"
>1</label>
<input type="radio" name="rating" id="rate2"><label for="rate2"
>2</label>
<input type="radio" name="rating" id="rate3"><label for="rate3"
>3</label>
<input type="radio" name="rating" id="rate4"><label for="rate4"
>4</label>
<input type="radio" name="rating" id="rate5"><label for="rate5"
>5</label>
<input type="radio" name="rating" id="rate6"><label for="rate6"
>6</label>
<input type="radio" name="rating" id="rate7"><label for="rate7"
>7</label>
<input type="radio" name="rating" id="rate8"><label for="rate8"
>8</label>
<input type="radio" name="rating" id="rate9"><label for="rate9"
>9</label>
<input type="radio" name="rating" id="rate10"><label for="rate10"
>10</label>
<div class="bottom">
<a href="index.html"><button class="btn1">Next</button></a>
</div>
</div>
</body>
</html>
解决方案
问题是这条规则:
input#rate1:checked ~ label{
background-color: blue;
}
这种方式你指的是所有的标签label
。但是你只需要引用当前的,所以你需要添加相应的属性,像这样:
input#rate1:checked ~ label[for="rate1"]{
background-color: blue;
}
有必要吗?
body{
margin: 0px;
padding: 0px;
background-color: bisque;
overflow: hidden;
}
.head{
text-align: center;
transform: translate(20px, 150px);
}
.rating{
transform: translate(320px, 200px);
}
.rating label{
cursor: pointer;
margin-right: 30px;
border: 1px solid #000;
border-radius: 25px;
padding: 5px 10px;
background-color: white;
}
.rating input{
display: none;
}
.labels{
display: flex;
list-style: none;
}
input#rate0:checked ~ label[for="rate0"]{
background-color: blue;
}
input#rate1:checked ~ label[for="rate1"]{
background-color: blue;
}
input#rate2:checked ~ label[for="rate2"]{
background-color: blue;
}
input#rate3:checked ~ label[for="rate3"]{
background-color: blue;
}
input#rate4:checked ~ label[for="rate4"]{
background-color: blue;
}
input#rate5:checked ~ label[for="rate5"]{
background-color: blue;
}
input#rate6:checked ~ label[for="rate6"]{
background-color: blue;
}
input#rate7:checked ~ label[for="rate7"]{
background-color: blue;
}
input#rate8:checked ~ label[for="rate8"]{
background-color: blue;
}
input#rate9:checked ~ label[for="rate9"]{
background-color: blue;
}
input#rate10:checked ~ label[for="rate10"]{
background-color: blue;
}
.bottom{
transform: translate(300px, 150px);
}
<body>
<div class="head">
<h1>On a Scale From 0-10, How Are You Feeling?</h1>
</div>
<div class="labels">
<ul>
<li>Rock Bottom</li>
<li>Terrible</li>
</ul>
</div>
<div class="rating">
<input type="radio" name="rating" id="rate0"><label for="rate0"
>0</label>
<input type="radio" name="rating" id="rate1"><label for="rate1"
>1</label>
<input type="radio" name="rating" id="rate2"><label for="rate2"
>2</label>
<input type="radio" name="rating" id="rate3"><label for="rate3"
>3</label>
<input type="radio" name="rating" id="rate4"><label for="rate4"
>4</label>
<input type="radio" name="rating" id="rate5"><label for="rate5"
>5</label>
<input type="radio" name="rating" id="rate6"><label for="rate6"
>6</label>
<input type="radio" name="rating" id="rate7"><label for="rate7"
>7</label>
<input type="radio" name="rating" id="rate8"><label for="rate8"
>8</label>
<input type="radio" name="rating" id="rate9"><label for="rate9"
>9</label>
<input type="radio" name="rating" id="rate10"><label for="rate10"
>10</label>
<div class="bottom">
<a href="index.html"><button class="btn1">Next</button></a>
</div>
</div>
</body>
推荐阅读
- javascript - Javascript asyn/await 未按预期运行
- mysql - Mysql 读取解析在 cli 中给出空白字符
- python - 尝试访问 networkx 中的节点属性时出现“keyerror”
- c# - 使用 ShowDialog 时拥有一个新表单实例还是每次显示表单时都创建一个新表单更好?- C#
- ruby-on-rails - Rails & simple_form // 没有路线匹配 [PATCH] "/spots.10"
- c# - C# 在 JSON 文件中将 db null 值视为空
- c# - Is it possible to draw object with outline and fill at the same time?
- c - getline中ctr+c如何返回?
- variables - 如果未找到上游主机,则使用 proxy_pass 变量来防止 nginx 崩溃
- gradle - IntelliJ IDEA (2020.2):Gradle:初始化脚本“ijinit.gradle”的缓存条目似乎已损坏