jQuery 星级评级插件可清除评级星级点击的评级?

发布于 2024-11-05 09:52:23 字数 183 浏览 0 评论 0原文

这令人困惑,但我似乎找不到一个像样的 jQuery 星级评分插件,可以在您单击评分星级时清除评分(例如:您评分 3 颗星,再次单击第三颗星,并且评分被删除) )。我一直在寻找在星星左侧有那个愚蠢的“清除评级”按钮的插件。 当我使用原型时,我使用 ratingbox,它运行良好,但我找不到 jQuery 等效项。

我将非常感谢任何建议!

This is baffling, but I can't seem to find a decent jQuery star rating plugin that clears the rating when you click on a rated star (as in: you rate 3 stars, you click the 3rd star again, and the rating is removed). I keep finding plugins that have that silly "clear rating" button to the left of the stars.
When I was using prototype I was using ratingbox, which worked well, but I can't find a jQuery equivalent.

I would greatly appreciate any recommendations!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情愿 2024-11-12 09:52:23

如果您不需要任何特殊功能,可以使用我的代码。如果需要的话,应该很容易将其转换为插件。

如果不需要悬停跟踪效果,只需删除 mouseentermouseleave 事件即可。

设置的评级保存在容器 div 上的数据属性中,您可以通过 $('. rating').data(' rating') 访问它。

查看现场演示,其中包含来自互联网的一些随机图像:http://jsfiddle.net/abPFF/1/< /a>

HTML:

<div class="rating" data-rating="3">
    <div></div><div></div><div></div><div></div><div></div>
</div>

CSS:

.rating { display:inline-block; padding:0px; }
.rating div { display:inline-block; width:48px; height:48px; margin:0px; background-image: url('star-off.png');}

.selected { background-image: url('star.png') !important; }
.highlighted { background-image: url('star.png') !important; }

JavaScript:

function ShowRating($element, rating){
    $stars = $element.find('div');
    $stars.removeClass('selected highlighted');
    rating = parseInt(rating);
    if(rating < 1 || rating > $stars.length) return;

    $stars.eq(rating-1).addClass('selected')
        .prevAll().addClass('highlighted');
    return;
}

$('.rating').each(function(){
    var $this = $(this);
    ShowRating($this, $this.data('rating'));
}).bind({
    mouseleave: function(){
        var $this = $(this);
        ShowRating($this, $this.data('rating'));
    }
}).find('div').bind({
    mouseenter: function(){
        var $this = $(this);
        ShowRating($this.parent(), $this.index() + 1);
    },
    click: function(){
        var $this = $(this);
        var $parent = $this.parent();
        var idx = $this.index() + 1;
        if($parent.data('rating') == idx){
            // Remove rating
            ShowRating($parent, 0);
            $parent.data('rating', 0);
        } else {
            // Set rating
            ShowRating($parent, idx);
            $parent.data('rating', idx);
        }
    }
});

If you don't need any special functionality, you can use my code. Should be easy to convert it into a plugin if required.

If you don't need the hover tracking effect, just remove the mouseenter and mouseleave events.

The set rating is saved in a data attribute on the container div, you can access it via $('.rating').data('rating').

Check out a live demo with some random images from the internet: http://jsfiddle.net/abPFF/1/

HTML:

<div class="rating" data-rating="3">
    <div></div><div></div><div></div><div></div><div></div>
</div>

CSS:

.rating { display:inline-block; padding:0px; }
.rating div { display:inline-block; width:48px; height:48px; margin:0px; background-image: url('star-off.png');}

.selected { background-image: url('star.png') !important; }
.highlighted { background-image: url('star.png') !important; }

Javascript:

function ShowRating($element, rating){
    $stars = $element.find('div');
    $stars.removeClass('selected highlighted');
    rating = parseInt(rating);
    if(rating < 1 || rating > $stars.length) return;

    $stars.eq(rating-1).addClass('selected')
        .prevAll().addClass('highlighted');
    return;
}

$('.rating').each(function(){
    var $this = $(this);
    ShowRating($this, $this.data('rating'));
}).bind({
    mouseleave: function(){
        var $this = $(this);
        ShowRating($this, $this.data('rating'));
    }
}).find('div').bind({
    mouseenter: function(){
        var $this = $(this);
        ShowRating($this.parent(), $this.index() + 1);
    },
    click: function(){
        var $this = $(this);
        var $parent = $this.parent();
        var idx = $this.index() + 1;
        if($parent.data('rating') == idx){
            // Remove rating
            ShowRating($parent, 0);
            $parent.data('rating', 0);
        } else {
            // Set rating
            ShowRating($parent, idx);
            $parent.data('rating', idx);
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文