如何用JavaScript实现动态星星评分效果?

更新于
2026-09-23 20:07:28
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计372个文字,预计阅读时间需要2分钟。

如何用JavaScript实现动态星星评分效果?

原文示例为:本文字例为大家庭分享了js实现星星打分的具体代码,供大家参考,具体内容如下+最终效果如下+HTML部分+div class=container小主的评价:/div span span+css样式+style+.star { font-size: 20px; color: gol; }

改写后内容:大家庭分享js实现星星评分的代码,供参考。代码如下:+最终效果呈现:+HTML结构+div class=container+小主评价:/div span span+CSS样式+style+.star { font-size: 20px; color: gold; }

本文实例为大家分享了js实现星星打分的具体代码,供大家参考,具体内容如下

最终效果如下

如何用JavaScript实现动态星星评分效果?

html部分

<div class="container">小主的评价:</div> <span></span>

css样式

<style> .star { font-size: 20px; color: gold; cursor: pointer; } .container { display: inline-block; text-align: center; } </style>

js部分

<script src="img.558idc.com/uploadfile/allimg/210405/1ZA55Y5-1.jpg"></script> <script> $(function () { let count = 5 let score = 3 for (let i = 0; i < count; i++) { let star = $("<i/>").addClass("iconfont").addClass("star") if (i < score) star.addClass("icon-xingxing") else star.addClass("icon-xingxing1") $(".container").append(star) } $(".star").mouseenter(function () { let index = $(this).index() $(".star").each(function (i) { if (i <= index) $(this).addClass("icon-xingxing").removeClass("icon-xingxing1") else $(this).addClass("icon-xingxing1").removeClass("icon-xingxing") }) }) $(".star").mouseleave(function () { $(".star").each(function (i) { if (i < score) $(this).addClass("icon-xingxing").removeClass("icon-xingxing1") else $(this).addClass("icon-xingxing1").removeClass("icon-xingxing") }) }) $(".star").click(function () { score = $(this).index() + 1 $("span").html(`${score}分`) }) $("span").html(`${score}分`) }) </script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计372个文字,预计阅读时间需要2分钟。

如何用JavaScript实现动态星星评分效果?

原文示例为:本文字例为大家庭分享了js实现星星打分的具体代码,供大家参考,具体内容如下+最终效果如下+HTML部分+div class=container小主的评价:/div span span+css样式+style+.star { font-size: 20px; color: gol; }

改写后内容:大家庭分享js实现星星评分的代码,供参考。代码如下:+最终效果呈现:+HTML结构+div class=container+小主评价:/div span span+CSS样式+style+.star { font-size: 20px; color: gold; }

本文实例为大家分享了js实现星星打分的具体代码,供大家参考,具体内容如下

最终效果如下

如何用JavaScript实现动态星星评分效果?

html部分

<div class="container">小主的评价:</div> <span></span>

css样式

<style> .star { font-size: 20px; color: gold; cursor: pointer; } .container { display: inline-block; text-align: center; } </style>

js部分

<script src="img.558idc.com/uploadfile/allimg/210405/1ZA55Y5-1.jpg"></script> <script> $(function () { let count = 5 let score = 3 for (let i = 0; i < count; i++) { let star = $("<i/>").addClass("iconfont").addClass("star") if (i < score) star.addClass("icon-xingxing") else star.addClass("icon-xingxing1") $(".container").append(star) } $(".star").mouseenter(function () { let index = $(this).index() $(".star").each(function (i) { if (i <= index) $(this).addClass("icon-xingxing").removeClass("icon-xingxing1") else $(this).addClass("icon-xingxing1").removeClass("icon-xingxing") }) }) $(".star").mouseleave(function () { $(".star").each(function (i) { if (i < score) $(this).addClass("icon-xingxing").removeClass("icon-xingxing1") else $(this).addClass("icon-xingxing1").removeClass("icon-xingxing") }) }) $(".star").click(function () { score = $(this).index() + 1 $("span").html(`${score}分`) }) $("span").html(`${score}分`) }) </script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。