html - 如何让两个 CSS 评级星形小部件在单击时互不影响

标签 html css

感谢您的关注...

请看下面fiddle显示两个仅 CSS 评级星级小部件或下面的代码。当您在第二个小部件上选择评级时,出于某种原因,它总是会更改第一个小部件上的评级。知道错误出在我的 CSS 中吗?对不起,如果这是一个基本问题,我对 CSS 还不是很好。谢谢!

HTML:

<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="rating" id="star5" value="5">
  <label for="star5"></label>
  <input type="radio" name="rating" id="star4" value="4">
  <label for="star4"></label>
  <input type="radio" name="rating" id="star3" value="3">
  <label for="star3"></label>
  <input type="radio" name="rating" id="star2" value="2">
  <label for="star2"></label>
  <input type="radio" name="rating" id="star1" value="1">
  <label for="star1"></label>
</div>
</br>
<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="rating" id="star5" value="5">
  <label for="star5"></label>
  <input type="radio" name="rating" id="star4" value="4">
  <label for="star4"></label>
  <input type="radio" name="rating" id="star3" value="3">
  <label for="star3"></label>
  <input type="radio" name="rating" id="star2" value="2">
  <label for="star2"></label>
  <input type="radio" name="rating" id="star1" value="1">
  <label for="star1"></label>
</div>

CSS:

.stars{
    width: 130px;
    height: 26px;
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 0 repeat-x;
    position: relative;
}

.stars .rating{
    height: 26px;
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -26px repeat-x;
}

.stars input{
    display: none;
}

.stars label{
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    height: 26px;
    width: 130px;
    cursor: pointer;
}
.stars:hover label{
    display: block;
}
.stars label:hover{
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -52px repeat-x;
}

.stars label + input + label{width: 104px;}
.stars label + input + label + input + label{width: 78px;}
.stars label + input + label + input + label + input + label{width: 52px;}
.stars label + input + label + input + label + input + label + input + label{width: 26px;}

.stars input:checked + label{
    display: block;
    background: url(http://sandbox.bumbu.ru/ui/external/stars.png) 0 -52px repeat-x;
}

最佳答案

Id's are unique在 HTML 中。您不能在页面上拥有多个具有相同 ID 的元素并期望它能够正常运行。您所看到的行为就是例证。由于您的标签将 for 属性设置为 id,浏览器将检查输入的第一个它“看到”的 id,因为它只需要一个。

每个 radio 组还需要有一个唯一的名称属性,否则所有 radio 将被视为一个单独的组,并且只能在页面上选择一颗星。

因此您需要更改第二组 radio 的 ID:

<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="rating" id="star5" value="5">
  <label for="star5"></label>
  <input type="radio" name="rating" id="star4" value="4">
  <label for="star4"></label>
  <input type="radio" name="rating" id="star3" value="3">
  <label for="star3"></label>
  <input type="radio" name="rating" id="star2" value="2">
  <label for="star2"></label>
  <input type="radio" name="rating" id="star1" value="1">
  <label for="star1"></label>
</div>
</br>
<div class="stars">
  <div class="rating" style="width:65%"></div>
  <input type="radio" name="ratinga" id="star5a" value="5">
  <label for="star5a"></label>
  <input type="radio" name="ratinga" id="star4a" value="4">
  <label for="star4a"></label>
  <input type="radio" name="ratinga" id="star3a" value="3">
  <label for="star3a"></label>
  <input type="radio" name="ratinga" id="star2a" value="2">
  <label for="star2a"></label>
  <input type="radio" name="ratinga" id="star1a" value="1">
  <label for="star1a"></label>
</div>

关于html - 如何让两个 CSS 评级星形小部件在单击时互不影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242258/

相关文章:

jquery - 我如何用 JQuery 用 <span> 包装一个 ul 列表中的未知单词?

javascript - 使用 javascript 问题更改 flexbox 宽度

html - <div> 标签在 Chrome 中有效,但在 Firefox/IE 中无效

html - 按比例缩放所有图像以适合容器

html - overflow-x 无法正常工作

javascript - 如何将网页的固定宽高设置为所用屏幕的最大宽高?

javascript - 如何在 Knockout 中绑定(bind) Html 5 视频控件属性

php - 压缩 CSS/JS/HTML

JAVA加载页面后获取url内容

javascript - 在鼠标悬停时淡出图片,然后淡入不同的图片