gpt4 book ai didi

javascript - 显示选择 5 星级

转载 作者:行者123 更新时间:2023-11-28 17:09:35 26 4
gpt4 key购买 nike

我需要配置一个 5 星评级系统。我已准备好显示星星的 css/html。我需要的是,当用户选择一颗星时,它应该将其显示为选中状态,但当前并未显示。我将使用 JS/Jquery/HTML 或 CSS。

下面是我的代码:

<html>

<head>
<title>5 star Rating!</title>
<style>
.rating {
unicode-bidi: bidi-override;
direction: rtl;
text-align: center;
}

.rating>span {
display: inline-block;
position: relative;
/*width: 1.1em;*/
}

.rating>span:hover,
.rating>span:hover~span {
color: transparent;
}

.rating>span:hover:before,
.rating>span:hover~span:before {
content: "\2605";
position: absolute;
left: 0;
color: gold;
cursor: pointer;
}

span {
font-size: 120px;
color: #f5f5f5;
text-shadow: 0px 5px 5px #000;
}

body {
background-image: url('https://sap.quickbase.com/up/biv7kj2ug/g/rh/eh/va/canvas-ptrn.jpg');
}

.wrapper {
background-color: #55A0EF;
height: 175px;
width: 100%;
opacity: 0.7;
box-shadow: 0px 2px 3px #000;
left: 0px;
right: 0px;
position: absolute;
}

h1 {
text-shadow: -1px -1px 0px #000, 1px 1px 0px #fff;
color: #535353;
font-family: Arial Narrow, Helvetica, sans-serif;
font-size: 100px;
text-transform: uppercase;
letter-spacing: -5px;
opacity: 0.7;
}
</style>

</head>

<body>
<center>
<h1>Star Rating</h1><br />
<div class="wrapper">
<div class="rating">
<span>&#9733;</span><span>&#9733;</span><span>&#9733;</span><span>&#9733;</span><span>&#9733;</span>
</div>
</div>
</center>
</body>

</html>

最佳答案

我应用了@K.B.M.的代码。看演示:

$('.star').on('click', function(){
$('.star').addClass('selected');
var count = $(this).attr('name');
for (var i=0; i<count-1; i++){
$('.star').eq(i).removeClass('selected');
}
});
.rating {
unicode-bidi: bidi-override;
direction: rtl;
text-align: center;
}
.rating > span {
display: inline-block;
position: relative;
/*width: 1.1em;*/
}
.rating > span:hover,
.rating > span:hover ~ span {
color: transparent;
}
.rating > span:hover:before,
.rating > span:hover ~ span:before {
content: "\2605";
position: absolute;
left: 0;
color: gold;
cursor: pointer;
}

span {
font-size:120px;
color:#f5f5f5;
text-shadow: 0px 5px 5px #000;
}


body {
background-image: url('https://sap.quickbase.com/up/biv7kj2ug/g/rh/eh/va/canvas-ptrn.jpg');
}

.wrapper {
background-color: #55A0EF;
height:175px;
width: 100%;
opacity: 0.7;
box-shadow: 0px 2px 3px #000;
left: 0px;
right: 0px;
position: absolute;
}

h1 {
text-shadow: -1px -1px 0px #000, 1px 1px 0px #fff;
color: #535353;
font-family: Arial Narrow, Helvetica, sans-serif;
font-size: 100px;
text-transform: uppercase;
letter-spacing: -5px;
opacity: 0.7;
}

.selected{color:gold;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<body>
<center>
<h1>Star Rating</h1><br />
<div class="wrapper">
<div class="rating">
<span class="star" name="1">&#9733;</span>
<span class="star" name="2">&#9733;</span>
<span class="star" name="3">&#9733;</span>
<span class="star" name="4">&#9733;</span>
<span class="star" name="5">&#9733;</span>
</div>
</div>
</center>
</body>

添加了这个CSS

.selected{color:gold;}

和javascript

$('.star').on('click', function(){
$('.star').addClass('selected');
var count = $(this).attr('name');
for (var i=0; i<count-1; i++){
$('.star').eq(i).removeClass('selected');
}
});

关于javascript - 显示选择 5 星级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29530054/

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