我正在嘗試制作一個星級評分系統,我設法制作了它,以便您可以將星星變成黃色。當我再次單擊星星時,它們不會變白。換句話說,我需要幫助切換星星,讓它們改變顏色。我是html的初學者,所以我不知道問題是什么。任何幫助,將不勝感激!這是我的代碼:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Star Rating</title>
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script>
$(function() {
$(".n").click(function() {
$(this).attr('src', 'star_checked.png');
$(this).removeClass("n");
$(this).addClass("y");
});
$(".y").click(function() {
$(this).attr('src', 'star.png');
$(this).removeClass("y");
$(this).addClass("n");
});
});
</script>
<style>
div#rating {
background-color: #eeeeee;
width: 200px;
height: 500px;
border-radius: 20px;
}
</style>
</head>
<body>
<div class="stars">
<img class="y" src="star_checked.png" alt="" width="100" height="100">
<img class="y" src="star_checked.png" alt="" width="100" height="100">
<img class="n" src="star.png" alt="" width="100" height="100">
<img class="n" src="star.png" alt="" width="100" height="100">
<img class="n" src="star.png" alt="" width="100" height="100">
</div>
</body>
</html>
這是我的圖片:白星 黃星
uj5u.com熱心網友回復:
Here is the same scenario you're trying https://codepen.io/Leoschmittk/pen/RwoQYBO
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/388808.html
