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

2026-04-02 23:481阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

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

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

原文示例:本文字例为大海分享了js实现星星打分的具体代码,供大家参考,具体内容如下:最终效果如下:部分: 小主子的评价:

css样式: .star { font-size: 20px; color: gold; }

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

最终效果如下

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部分

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

<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>

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

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

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

原文示例:本文字例为大海分享了js实现星星打分的具体代码,供大家参考,具体内容如下:最终效果如下:部分: 小主子的评价:

css样式: .star { font-size: 20px; color: gold; }

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

最终效果如下

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部分

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

<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>

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