转换 5 星级脚本以使用 jQuery v1.3.2 时出现问题

发布于 2024-08-19 03:38:43 字数 4502 浏览 4 评论 0原文

我在转换这个 5 星级评级脚本时遇到了麻烦,该脚本与日期发布的 jquery 修订版本完美配合:2006-09-07 10:12:12 +0200(Do,2006 年 9 月 7 日)$* $Rev:276 $

这是我认为 addclass、removeclass、.extend 函数是问题所在,但我似乎无法成功将其转换为与 jquery v1.3.2 一起使用?

真的需要这个尽快工作。

感谢您的帮助... stackoverflow.com 摇滚!

这是你的称呼:

<script type="text/javascript">
$(document).ready(function() {
$('#rate1').rating('postid', { maxvalue:5 });
});
</script>
<div id="rate1" class="rating">&nbsp;</div>

这是 css:

.rating {
    cursor: pointer;
    margin: 0em;
    display: block;
    float:left;
}
.rating:after {
    content: '.';
    display: block;
    height: 0;
    width: 0;
    clear: both;
    visibility: hidden;
}
.cancel,
.star {
    float: left;
    width: 25px;
    height: 25px;
    overflow: hidden;
    text-indent: -999em;
    cursor: pointer;
}
.cancel,
.cancel a {background: url(delete.gif) no-repeat 0 -25px;}

.star,
.star a {background: url(star.gif) no-repeat 0 0px;}

.cancel a,
.star a {
    display: block;
    width: 100%;
    height: 100%;
    background-position: 0 0px;
}

div.rating div.on a {
    background-position: 0 -25px;
}
div.rating div.hover a,
div.rating div a:hover {
    background-position: 0 -50px;
}

这是评级脚本:

function postrating(i) {
    $("#rate_result").html(i);
}

function doSubmit() {
         var rating = $('#result').html();
         var reviewid = $('#reviewid').html();
         if(rating == '1') {
            alert('Are you kidding me!');
         }
         if(rating == '5') {
            alert('Thanks for the 5 star rating!');
         }
         alert(rating);
         alert(reviewid);
}

$.fn.rating = function(id, options) {
    if(id == null) return;
    var settings = {
        id       : id, // post changes to 
        maxvalue  : 5,   // max number of stars
        curvalue  : 0    // number of selected stars
    };
    if(options) {
       $.extend(settings, options);
    };
    $.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
    var container = jQuery(this);
    $.extend(container, {
            averageRating: settings.curvalue,
            id: settings.id
        });
    for(var i= 0; i <= settings.maxvalue ; i++){
        var size = i
        if (i == 0) {
            if(settings.cancel == true){
                 var div = '';
                 container.append(div);
            }
        } 
        else {
             var div = '<div class="star"><a href="javscript:void(0)" onclick="javscript:postrating('+ i +');" title="'+i+' out of 5">'+i+'</a></div>';
             container.append(div);
        }
    }
    var stars = $(container).children('.star');
    stars
            .mouseover(function(){
                event.drain();
                event.fill(this);
            })
            .mouseout(function(){
                event.drain();
                event.reset();
            })
            .focus(function(){
                event.drain();
                event.fill(this)
            })
            .blur(function(){
                event.drain();
                event.reset();
            });
            stars.click(function(){
            if(settings.cancel == true) {
            settings.curvalue = stars.index(this) + 1;
            $.post(container.id, {
                "rating": $(this).children('a')[0].href.split('#')[1] 
            });
            return false;
            }
            else if(settings.maxvalue == 1) {
            settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
            $(this).toggleClass('on');
            $.post(container.id, {
                "rating": $(this).children('a')[0].href.split('#')[1]
            });
            return false;
        }
        return true;
    });
    var event = {
        fill: function(el){ // fill to the current mouse position.
            var index = stars.index(el) + 1;
            stars
                .children('a').css('width', '100%').end()
                .lt(index).addClass('hover').end();
        },
        drain: function() { // drain all the stars.
            stars
                .filter('.on').removeClass('on').end()
                .filter('.hover').removeClass('hover').end();
        },
        reset: function(){ // Reset the stars to the default index.
            stars.lt(settings.curvalue).addClass('on').end();
        }
    }        
    event.reset();
    return(this);
}

I'm having trouble converting this 5 star rating script that works perfect with jquery revision put out on Date: 2006-09-07 10:12:12 +0200 (Do, 07 Sep 2006) $* $Rev: 276 $

It's the addclass, removeclass, .extend functions that I think are the problem but I can't seem to successfully convert it to work with jquery v1.3.2?

Really need this to work asap.

Thanks for all your help... stackoverflow.com ROCKS!

Heres how you call it:

<script type="text/javascript">
$(document).ready(function() {
$('#rate1').rating('postid', { maxvalue:5 });
});
</script>
<div id="rate1" class="rating"> </div>

Heres the css:

.rating {
    cursor: pointer;
    margin: 0em;
    display: block;
    float:left;
}
.rating:after {
    content: '.';
    display: block;
    height: 0;
    width: 0;
    clear: both;
    visibility: hidden;
}
.cancel,
.star {
    float: left;
    width: 25px;
    height: 25px;
    overflow: hidden;
    text-indent: -999em;
    cursor: pointer;
}
.cancel,
.cancel a {background: url(delete.gif) no-repeat 0 -25px;}

.star,
.star a {background: url(star.gif) no-repeat 0 0px;}

.cancel a,
.star a {
    display: block;
    width: 100%;
    height: 100%;
    background-position: 0 0px;
}

div.rating div.on a {
    background-position: 0 -25px;
}
div.rating div.hover a,
div.rating div a:hover {
    background-position: 0 -50px;
}

And heres the rating script:

function postrating(i) {
    $("#rate_result").html(i);
}

function doSubmit() {
         var rating = $('#result').html();
         var reviewid = $('#reviewid').html();
         if(rating == '1') {
            alert('Are you kidding me!');
         }
         if(rating == '5') {
            alert('Thanks for the 5 star rating!');
         }
         alert(rating);
         alert(reviewid);
}

$.fn.rating = function(id, options) {
    if(id == null) return;
    var settings = {
        id       : id, // post changes to 
        maxvalue  : 5,   // max number of stars
        curvalue  : 0    // number of selected stars
    };
    if(options) {
       $.extend(settings, options);
    };
    $.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
    var container = jQuery(this);
    $.extend(container, {
            averageRating: settings.curvalue,
            id: settings.id
        });
    for(var i= 0; i <= settings.maxvalue ; i++){
        var size = i
        if (i == 0) {
            if(settings.cancel == true){
                 var div = '';
                 container.append(div);
            }
        } 
        else {
             var div = '<div class="star"><a href="javscript:void(0)" onclick="javscript:postrating('+ i +');" title="'+i+' out of 5">'+i+'</a></div>';
             container.append(div);
        }
    }
    var stars = $(container).children('.star');
    stars
            .mouseover(function(){
                event.drain();
                event.fill(this);
            })
            .mouseout(function(){
                event.drain();
                event.reset();
            })
            .focus(function(){
                event.drain();
                event.fill(this)
            })
            .blur(function(){
                event.drain();
                event.reset();
            });
            stars.click(function(){
            if(settings.cancel == true) {
            settings.curvalue = stars.index(this) + 1;
            $.post(container.id, {
                "rating": $(this).children('a')[0].href.split('#')[1] 
            });
            return false;
            }
            else if(settings.maxvalue == 1) {
            settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
            $(this).toggleClass('on');
            $.post(container.id, {
                "rating": $(this).children('a')[0].href.split('#')[1]
            });
            return false;
        }
        return true;
    });
    var event = {
        fill: function(el){ // fill to the current mouse position.
            var index = stars.index(el) + 1;
            stars
                .children('a').css('width', '100%').end()
                .lt(index).addClass('hover').end();
        },
        drain: function() { // drain all the stars.
            stars
                .filter('.on').removeClass('on').end()
                .filter('.hover').removeClass('hover').end();
        },
        reset: function(){ // Reset the stars to the default index.
            stars.lt(settings.curvalue).addClass('on').end();
        }
    }        
    event.reset();
    return(this);
}

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

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

发布评论

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

评论(1

無心 2024-08-26 03:38:43

.lt() 不是一个正确的函数。您正在寻找 :lt() 选择器。使用 .filter 才能使用 :lt() 选择器。检查您编写 .lt(foo) 的代码,改写如下内容:

.filter(":lt(" + foo + ")")

或者,您可能需要查看 .prevAll()

.lt() is not a proper function. You are looking for the :lt() selector. Use a .filter in order to use the :lt() selector. Check your code where you have written .lt(foo) and instead write something like:

.filter(":lt(" + foo + ")")

Alternatively, you may want to look at .prevAll()

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