jQuery 评级系统

发布于 2024-11-29 05:48:24 字数 611 浏览 0 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦回梦里 2024-12-06 05:48:25

为什么不直接使用别人的插件呢?例如 http://www.fyneworks.com/jquery/star- rating/

Why not just use someone's plugin? e.g. http://www.fyneworks.com/jquery/star-rating/

寂寞花火° 2024-12-06 05:48:24

如果您在点击之前使用它作为预览,那么您可能需要像这样使用悬停功能:

$('#stars .star').hover(
    function() {
        $(this).prevAll().andSelf().addClass('on');
        $(this).nextAll().removeClass('on');
    },
    function() {
        $(this).parent().children().removeClass('on');
    }
);

这与 jsFiddle 中的 HTML 一致,您可以在其中看到它的工作原理: http://jsfiddle.net/jfriend00/aRTzQ/

我还建议使用 addClass()removeClass() 而不是直接操作属性,因为它可以让您更灵活地在对象上拥有其他类,并且更具可读性。

要使点击粘性,以便它记住该设置,您可以使用如下内容: http://jsfiddle.net /jfriend00/QxADg/。相应的 CSS/HTML 就在小提琴中。

$('#stars .star').hover(
    function() {
        $(this).prevAll().andSelf().addClass('preview');
        $(this).nextAll().removeClass('preview');
    },
    function() {
        $(this).parent().children().removeClass('preview');
    }
);

$('#stars .star').click(function() {
    $(this).parent().children().removeClass('on');
    $(this).prevAll().andSelf().addClass('on');
});

$('#stars').hover(
    function() {$(this).addClass("inPreview");},
    function() {$(this).removeClass("inPreview");}
);

If you're using this as a preview before a click, then you may want to use the hover function like this:

$('#stars .star').hover(
    function() {
        $(this).prevAll().andSelf().addClass('on');
        $(this).nextAll().removeClass('on');
    },
    function() {
        $(this).parent().children().removeClass('on');
    }
);

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() and removeClass() 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.

$('#stars .star').hover(
    function() {
        $(this).prevAll().andSelf().addClass('preview');
        $(this).nextAll().removeClass('preview');
    },
    function() {
        $(this).parent().children().removeClass('preview');
    }
);

$('#stars .star').click(function() {
    $(this).parent().children().removeClass('on');
    $(this).prevAll().andSelf().addClass('on');
});

$('#stars').hover(
    function() {$(this).addClass("inPreview");},
    function() {$(this).removeClass("inPreview");}
);
灯角 2024-12-06 05:48:24

那里有很多插件,例如 PPrice 链接到的插件。我同意其他人的观点,这将是更好的方法,除非他们由于某种原因不能满足您的需求。

如果您想让这段代码工作或者您只是为了学习而这样做,也许这就是您想要做的?

您可以将鼠标悬停在“星星”(框)上以查看颜色变化。

http://jsfiddle.net/pW3Sn/

$('#stars > div').mouseover(function() {
        $(this).prevAll().andSelf().attr('class', 'on');  
        $(this).nextAll().attr('class', 'off');  
});

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/

$('#stars > div').mouseover(function() {
        $(this).prevAll().andSelf().attr('class', 'on');  
        $(this).nextAll().attr('class', 'off');  
});
子栖 2024-12-06 05:48:24

其实你可以用纯CSS来实现。

HTML 代码:

<div id="stars">
    <div class="star">
        <div class="star">
            <div class="star">
                <div class="star">
                    <div class="star"></div>
                </div>
            </div>
        </div>
    </div>
</div>

和 CSS:

#stars {
    height: 16px; /* height of stars block */
}
#stars .star {
    height: 16px; /* height of one star */
    padding-left: 16px; /* width of one star */
    background-image: url('http://url.to.your.star.image');
    float: left;
}
#stars .star:hover, 
#stars .star :hover {
    background-position: 0px -16px; /* toggle image */
}

一个工作示例: http://jsfiddle.net/4CfzD/

Actually you can implement it using pure CSS.

HTML code:

<div id="stars">
    <div class="star">
        <div class="star">
            <div class="star">
                <div class="star">
                    <div class="star"></div>
                </div>
            </div>
        </div>
    </div>
</div>

And CSS:

#stars {
    height: 16px; /* height of stars block */
}
#stars .star {
    height: 16px; /* height of one star */
    padding-left: 16px; /* width of one star */
    background-image: url('http://url.to.your.star.image');
    float: left;
}
#stars .star:hover, 
#stars .star :hover {
    background-position: 0px -16px; /* toggle image */
}

A working example: http://jsfiddle.net/4CfzD/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文