星级评分系统第二选项使用CSS更改第一个选项

时间:2017-07-09 23:15:59

标签: html css css-selectors

我有一个用html和css制作的星系。

我的问题是,当我改变第二个系统的评级时,第一个系统也会改变。我试图使用不同的类名,但显然我错过了一些东西。有什么建议吗?

HTML

<div style="padding-top:20px;" class="row">
  <div style="border-right: solid #4a98b5; width:290px;" class="col-
md-4"><p>Product Matches description</p></div>
  <div style="font-size:20px;color:#FFD700" class="col-md-4">
   <fieldset class="rating1">
  <input type="radio" id="star5" name="rating1" value="5" /><label class = "full" for="star5" ></label>
  <input type="radio" id="star4half" name="rating1" value="4.5" /><label class="half" for="star4half"></label>
  <input type="radio" id="star4" name="rating1" value="4" /><label class = "full" for="star4"></label>
  <input type="radio" id="star3half" name="rating1" value="3.5" /><label class="half" for="star3half"></label>
  <input type="radio" id="star3" name="rating1" value="3" /><label class = "full" for="star3"></label>
  <input type="radio" id="star2half" name="rating1" value="2.5" /><label class="half" for="star2half"></label>
  <input type="radio" id="star2" name="rating1" value="2" /><label class = "full" for="star2"></label>
  <input type="radio" id="star1half" name="rating1" value="1.5" /><label class="half" for="star1half"></label>
  <input type="radio" id="star1" name="rating1" value="1" /><label class = "full" for="star1"></label>
  <input type="radio" id="starhalf" name="rating1" value="0.5" /><label class="half" for="starhalf"></label>
</fieldset>
</div>
</div>

<div style="padding-top:20px;" class="row">
 <div style="border-right: solid #4a98b5; width:290px;" class="col-
md-4"><p>Rate your experience</p></div>
<div style="font-size:20px;color:#FFD700" class="col-md-4">
<fieldset class="rating2">
  <input type="radio" id="star5" name="rating2" value="5" /><label class = "full" for="star5" ></label>
  <input type="radio" id="star4half" name="rating2" value="4.5" /><label class="half" for="star4half"></label>
  <input type="radio" id="star4" name="rating2" value="4" /><label class = "full" for="star4"></label>
  <input type="radio" id="star3half" name="rating2" value="3.5" /><label class="half" for="star3half"></label>
  <input type="radio" id="star3" name="rating2" value="3" /><label class = "full" for="star3"></label>
  <input type="radio" id="star2half" name="rating2" value="2.5" /><label class="half" for="star2half"></label>
  <input type="radio" id="star2" name="rating2" value="2" /><label class = "full" for="star2"></label>
  <input type="radio" id="star1half" name="rating2" value="1.5" /><label class="half" for="star1half"></label>
  <input type="radio" id="star1" name="rating2" value="1" /><label class = "full" for="star1"></label>
  <input type="radio" id="starhalf" name="rating2" value="0.5" /><label class="half" for="starhalf"></label>
</fieldset>
</div>

CSS

@import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);

.rating1 {
 border: none;
 float: left;
 }

.rating1 > input { display: none; }
.rating1 > label:before {
  margin: 5px;
  font-size: 1.25em;
  font-family: FontAwesome;
  display: inline-block;
  content: "\f005";
}

.rating1 > .half:before {
  content: "\f089";
  position: absolute;
}

.rating1 > label {
  color: #ddd;
 float: right;
}


.rating1 > input:checked ~ label, /* show gold star when clicked */
.rating1:not(:checked) > label:hover, /* hover current star */
.rating1:not(:checked) > label:hover ~ label { color: #FFD700;  } /* hover previous stars in list */

.rating1 > input:checked + label:hover, /* hover current star when 
changing rating */
.rating1 > input:checked ~ label:hover,
.rating1 > label:hover ~ input:checked ~ label, /* lighten current 
selection */
.rating1 > input:checked ~ label:hover ~ label { color: #FFED85;  }

.rating2 {
  border: none;
  float: left;
}

.rating2 > input { display: none; }
.rating2 > label:before {
  margin: 5px;
  font-size: 1.25em;
  font-family: FontAwesome;
  display: inline-block;
  content: "\f005";
}

.rating2 > .half:before {
  content: "\f089";
  position: absolute;
}

.rating2 > label {
  color: #ddd;
  float: right;
}


.rating2 > input:checked ~ label, /* show gold star when clicked */
.rating2:not(:checked) > label:hover, /* hover current star */
.rating2:not(:checked) > label:hover ~ label { color: #FFD700;  } /* hover previous stars in list */

.rating2 > input:checked + label:hover, /* hover current star when changing rating */
.rating2 > input:checked ~ label:hover,
.rating2 > label:hover ~ input:checked ~ label, /* lighten current selection */
.rating2 > input:checked ~ label:hover ~ label { color: #FFED85;  }

请查看https://codepen.io/anon/pen/weYbzZ代码。

1 个答案:

答案 0 :(得分:1)

解决了问题!

虽然字段集具有不同的类名,但输入字段具有相同的名称,因此当第二个输入字段更改时,它会更改第一个字段。因此输入字段上的不同ID名称解决了问题