gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:24 25 4
gpt4 key购买 nike

感谢您的关注...

请看下面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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com