jQuery-星级评价

// 星级评价

    var StarRating = {
        score: 0,
        message: ['1分|很不满意', '2分|不满意', '3分|一般', '4分|满意', '5分|很满意'],
        obj: '.star_rating_wrapper',
        init: function (callback) {
            var that = this;
            var $ele = $(this.obj);
            var $star = $ele.find('span');
            $star.on({
                'click': function () {
                    that.score = $(this).index();
                    $(this).addClass('selected').siblings().removeClass('selected');
                    that.showMsg(that.score);
                    if (callback) {
                        callback.call(this, that.score);
                    }
                },
                'mouseover': function () {
                    $(this).addClass('on').siblings().removeClass('on').end().prevAll().addClass('on');
                },
                'mouseout': function () {
                    var score = that.score + 1;
                    $(this).parent().children().removeClass('on');
                    $(this).parent().find('span:lt(' + score + ')').addClass('on');
                }
            });
        },
        uninit: function () {
            var $ele = $(this.obj),
                $star = $ele.find('span');
            $star.off('click').off('mouseover').off('mouseout');
        },
        showTip: function (msg) {
            // TODO
        },
        showMsg: function (i) {
            var msg = this.message[i];
            $('#starMsg').remove();
            $('<span id="starMsg">' + msg + '</span>').insertAfter($(StarRating.obj));
        }
    };
    
    // demo:
    StarRating.init(function (i) {
        // console.log(i);
    });

    if($('.star_rating_wrapper').parent().hasClass('ci_yourEva_con')) {
        StarRating.uninit();
    }

 

posted @ 2013-02-01 10:11  LukeLin  阅读(424)  评论(0编辑  收藏  举报