jQuery 评级系统
我正在尝试从 0 开始制作一个评级系统,到目前为止我有两个课程,总共有 5 颗星,
它会显示该项目的评级,直到该人将鼠标悬停在它们上方,然后它会突出显示以前的星星并隐藏当前的评级,
<div id="stars">
<div class="on"></div>
<div class="on"></div>
<div class="on"></div>
<div class="off"></div>
<div class="off"></div>
</div>
到目前为止我得到的 JS
$('#stars').mouseover(function() {
$(this).children().prevAll().andSelf().attr('class', 'on');
$(this).nextAll().attr('class', 'on');
});
只突出显示所有的开始并搞乱一切,任何人都可以帮忙吗?工作时间太多却无法正常工作..
谢谢!
I'm trying to make a rating system from 0, so far I have two classes on and off, there are total of 5 stars,
It would show the rating of the item, until the person hovers over them, then it would hilight the previous stars and hide the current rating,
<div id="stars">
<div class="on"></div>
<div class="on"></div>
<div class="on"></div>
<div class="off"></div>
<div class="off"></div>
</div>
the JS I got so far
$('#stars').mouseover(function() {
$(this).children().prevAll().andSelf().attr('class', 'on');
$(this).nextAll().attr('class', 'on');
});
Which only hilights all the starts and messes up everything, anyone could help out? Working too many hours and just can't get it to work..
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接使用别人的插件呢?例如 http://www.fyneworks.com/jquery/star- rating/
Why not just use someone's plugin? e.g. http://www.fyneworks.com/jquery/star-rating/
如果您在点击之前使用它作为预览,那么您可能需要像这样使用悬停功能:
这与 jsFiddle 中的 HTML 一致,您可以在其中看到它的工作原理: http://jsfiddle.net/jfriend00/aRTzQ/
我还建议使用
addClass()
和removeClass()
而不是直接操作属性,因为它可以让您更灵活地在对象上拥有其他类,并且更具可读性。要使点击粘性,以便它记住该设置,您可以使用如下内容: http://jsfiddle.net /jfriend00/QxADg/。相应的 CSS/HTML 就在小提琴中。
If you're using this as a preview before a click, then you may want to use the hover function like this:
That goes with the HTML in this jsFiddle where you can see it work: http://jsfiddle.net/jfriend00/aRTzQ/
I'd also suggest using
addClass()
andremoveClass()
rather than manipulating the attribute directly as it gives you more flexibility to have other classes on the object and it's more readable.To make a click sticky so it then remembers that setting, you could use something like this: http://jsfiddle.net/jfriend00/QxADg/. The corresponding CSS/HTML is in the fiddle.
那里有很多插件,例如 PPrice 链接到的插件。我同意其他人的观点,这将是更好的方法,除非他们由于某种原因不能满足您的需求。
如果您想让这段代码工作或者您只是为了学习而这样做,也许这就是您想要做的?
您可以将鼠标悬停在“星星”(框)上以查看颜色变化。
http://jsfiddle.net/pW3Sn/
There's a lot of plugins out there, such as the one PPrice links to. I agree with others that that would be the better way to go unless they don't meet your needs for some reason.
If you want to make this code work or if you're just doing this for the learning, maybe this is what you're trying to do?
You can hover over the "stars" (boxes) to see the colour change.
http://jsfiddle.net/pW3Sn/
其实你可以用纯CSS来实现。
HTML 代码:
和 CSS:
一个工作示例: http://jsfiddle.net/4CfzD/
Actually you can implement it using pure CSS.
HTML code:
And CSS:
A working example: http://jsfiddle.net/4CfzD/