有什么方法可以压缩这个jquery 5星评级函数吗?
我正在尝试缩小这个范围......但无法找到办法。 基本上我想知道是否有任何方法可以将其压缩到 5 行左右?
感谢您的帮助!
$(function() {
$('.star-one').live("click",function() {
$("#rate_result").html('1');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-two').live("click",function() {
$("#rate_result").html('2');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-three').live("click",function() {
$("#rate_result").html('3');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star3").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-four').live("click",function() {
$("#rate_result").html('4');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star3").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star4").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-five').live("click",function() {
$("#rate_result").html('5');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star3").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star4").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star5").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
});
这是 HTML:
<div id="rate_result"></div>
<div id="stars-container">
<ul class='star-rate'>
<li id="star1">
<a href='#' title='' class="star-one" id="1">mmmm</a>
</li>
<li id="star2">
<a href='#' title='' class="star-two" id="2">try again</a>
</li>
<li id="star3">
<a href='#' title='' class="star-three" id="3">mmm not bad</a>
</li>
<li id="star4">
<a href='#' title='' class="star-four" id="4">this is cool ya!</a>
</li>
<li id="star5">
<a href='#' title='' class="star-five" id="5">very good</a>
</li>
</ul>
</div>
I'm trying to shrink this down... but can't figure out a way to do it.
Basically am wondering if theres any way to compress it down to 5 lines or so??
Thanks for your help!
$(function() {
$('.star-one').live("click",function() {
$("#rate_result").html('1');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-two').live("click",function() {
$("#rate_result").html('2');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-three').live("click",function() {
$("#rate_result").html('3');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star3").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-four').live("click",function() {
$("#rate_result").html('4');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star3").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star4").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
$('.star-five').live("click",function() {
$("#rate_result").html('5');
$("#star1").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star2").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star3").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star4").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
$("#star5").html('<img src="http://larsonreviews.com/rating/star_ena.gif" border="0">');
});
});
Here is the html:
<div id="rate_result"></div>
<div id="stars-container">
<ul class='star-rate'>
<li id="star1">
<a href='#' title='' class="star-one" id="1">mmmm</a>
</li>
<li id="star2">
<a href='#' title='' class="star-two" id="2">try again</a>
</li>
<li id="star3">
<a href='#' title='' class="star-three" id="3">mmm not bad</a>
</li>
<li id="star4">
<a href='#' title='' class="star-four" id="4">this is cool ya!</a>
</li>
<li id="star5">
<a href='#' title='' class="star-five" id="5">very good</a>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为每个星星添加一个名为 star 的类
add a class called star to each star
也许这会让您了解如何稍微压缩代码:
该代码当前将数字转换为星号,但可以轻松地进行修改以处理用于更改值的单击事件。
不需要为每个星星做单独的 img 元素,您唯一需要的是两个以星星作为背景图像的跨度,然后只需对这些跨度应用适当的宽度。
Perhaps this will give you an idea how you can compress your code a bit:
That code currently turns numbers to stars but can easily be adapted to handle click events for changing the value.
There's no need to do separate img elements for each star, only thing you need is two spans which have the stars as an background image and then just apply a proper width to those spans.
创建一个循环 1 直到 #rate_result 并输出
$("#star" + count).html('');
Create a loop for 1 until #rate_result and ouput
$("#star" + count).html('<img />');
第 1 步:
创建 1 个变量来保存 url 字符串:
然后用您的变量替换所有这些字符串。
Step 1:
Create 1 variable to hold the url string:
then replace all those strings with your variable.
未经测试,但我猜这会起作用。
Untested, but I'd guess this would work.
这是一些压缩:
Here's a bit of compression: