有一个JS文件,里面的方法其中一个是设置COOKIE的.但COOKIE是有了,却没值.为何呢下面一楼补充JS文件,我是这样调用的.compareFloat.clickCompareModel('*','*','*','*')参数是没问题的

解决方案 »

  1.   

    ****
    var compareFloat = new CompareFloat();
    compareFloat.init();
    function CompareFloat(){
        this.itemHeight = 150;//单元格高度
        this.top = 0;//层偏移值
        this.noPhoto = 'http://www1.pcauto.com.cn/2009/price/img/nopic120.gif';
        this.compareModels = [];
        this.rollState = 0;
        this.param = {path: '/',domain:'pcauto.com.cn'};
        
        this.init = function(){
            var me = this;
            if ($.cookie('compareModels')) {
                this.compareModels = $.cookie('compareModels').split("##");
            }
            $.each(this.compareModels, function(i, n) {
                var modelInfo = n.split('||');
                if (!modelInfo[2]) {
                    modelInfo[2] = this.noPhoto;
                }
                $('#proBoxUl>li.template').clone()
                        .attr('id', modelInfo[0])
                        .find('i:eq(0)>a').click(function() {
                    me.deleteCompareModel(modelInfo[0]);
                }).end()
                .find('i:eq(1)>a').attr('href', 'http://price.pcauto.com.cn/m' + modelInfo[0] + '/').end()
                .find('i:eq(1)>a>img').attr('src', modelInfo[2]).attr('alt', modelInfo[1]).attr('title', modelInfo[2]).end()
                .find('i:last>a').attr('href', 'http://price.pcauto.com.cn/m' + modelInfo[0] + '/').html(modelInfo[1]).end()
                .removeAttr('class').appendTo($('#proBoxUl'));
            });
            $('#compareModelCount').text(this.compareModels.length);
            if (this.compareModels.length > 0) {
                this.show();
                if (this.compareModels.length > 2) {
                    $('#rollUp,#rollDown').css('visibility', 'visible');
                }
            }
            $("#rollUp").click(function(){
                me.rollUp();
            });
            $("#rollDown").click(function(){
                me.rollDown();
            });
            $('#proBox').bind('mousewheel', function(event, delta) {
                if (delta > 0) {
                    me.rollUp();
                } else if (delta < 0 ) {
                    me.rollDown();
                }
                return false;
            });
            this.updateUI(true);
            //初始化车型对比选择框
            $.each(this.compareModels,function(i,n){
                var modelInfo = n.split('||');
                $("#compareModelChooser1_" + modelInfo[0]).attr("checked", true);
                $("#compareModelChooser2_" + modelInfo[0]).attr("checked", true);
            });
            $("input[name='compareModelChooser']").removeAttr('disabled');
        };
        this.rollDown = function(){
            var me = this;
            var scrollNum = this.compareModels.length > 2 ? this.compareModels.length - 2 : 0;
            if (this.rollState != 1 && this.top > -(this.itemHeight * scrollNum)) {
                this.top -= this.itemHeight;
                this.rollState = 1;
                $("#proBoxUl").animate({top: this.top + "px"}, "normal", "swing", function(){
                    me.rollState = 0;
                    me.updateUI();
                });
            }
        };
        this.rollBottom = function(){
            var me = this;
            var scrollNum = this.compareModels.length > 2 ? this.compareModels.length - 2 : 0;
            if (this.rollState != 1 && this.top > -(this.itemHeight * scrollNum)) {
                this.top = -this.itemHeight * (this.compareModels.length - 2);
                this.rollState = 1;
                $("#proBoxUl").animate({top: this.top + "px"}, "normal", "swing", function(){
                    me.rollState = 0;
                    me.updateUI();
                });
            }
        };
        this.rollUp = function(){
            if (this.rollState != 1 && this.top <= -this.itemHeight) {
                var me = this;
                this.top += this.itemHeight;
                this.rollState = 1;
                $("#proBoxUl").animate({top: this.top + "px"}, "normal", "swing", function(){
                    me.rollState = 0;
                    me.updateUI();
                });
            }
        };
        this.rollTop = function(){
            if (this.rollState != 1 && this.top <= -this.itemHeight) {
                var me = this;
                this.top = 0;
                this.rollState = 1;
                $("#proBoxUl").animate({top: this.top + "px"}, "normal", "swing", function(){
                    me.rollState = 0;
                    me.updateUI();
                });
            }
        };
        this.show = function(){
            $("#contrastBox").show();
            $("#contrastBtn").hide();
        };
        this.hide = function(){
            $("#contrastBox").hide();
            $("#contrastBtn").show();
        };
        this.updateUI = function (init){
            if (this.compareModels.length > 2) {
                $('#rollUp,#rollDown').css('visibility','visible');
                if (this.top == 0) {
                    $('#rollUp').css('visibility','hidden');
                } else if (this.top == - this.itemHeight * (this.compareModels.length - 2)) {
                    $('#rollDown').css('visibility','hidden');
                } else if (this.top < - this.itemHeight * (this.compareModels.length - 2)) {
                    this.rollUp();
                }
            } else {
                this.rollTop();
                $("#proBoxUl").css('top', this.top+'px');
                $('#rollUp,#rollDown').css('visibility','hidden');
            }
            if (this.compareModels.length > 0 && !init) {
                this.show();
            }else {
                this.hide();
            }
        };
        this.addCompareModel = function(mid, mname, thumb){
            if (this.compareModels.length >= 4) {
                alert('最多只能选择4款车型进行比较 :-)');
                return false;
            }
            var hasChosen = false;
            $.each(this.compareModels, function(i, n){
                var modelInfo = n.split('||');
                if (modelInfo[0] == mid) {
                    hasChosen = true;
                    return false;
                }
            });
            if (hasChosen) {
                alert('该车型已经存在于车型对比篮中 :-)');
                return false;
            }
            var modelInfo = [mid, mname, thumb];
            if (!modelInfo[2]) {
                modelInfo[2] = this.noPhoto;
            }
            var me = this;
            $('#proBoxUl>li.template').clone()
                .attr('id', modelInfo[0])
                .find('i:eq(0)>a').click(function(){me.deleteCompareModel(modelInfo[0]);}).end()
                .find('i:eq(1)>a').attr('href','http://price.pcauto.com.cn/m'+modelInfo[0]+'/').end()
                .find('i:eq(1)>a>img').attr('src',modelInfo[2]).attr('alt',modelInfo[1]).attr('title',modelInfo[2]).end()
                .find('i:last>a').attr('href','http://price.pcauto.com.cn/m'+modelInfo[0]+'/').html(modelInfo[1]).end()
                .removeAttr('class').appendTo($('#proBoxUl'));
            this.compareModels.push(mid + '||' + mname + '||' + thumb);
            this.updateUI();
            $('#compareModelCount').text(this.compareModels.length);
            $.cookie('compareModels',this.compareModels.join('##'),this.param);
            //如果当前位置没有能显示新添加项, 则滚动到新添加项
            var a = -(this.top / 150);
            var b = this.compareModels.length;
            if (a < b-2) {
                this.rollBottom();
            }        return true;
        };    this.deleteCompareModel = function (mid){
            $('#proBoxUl>li[id='+mid+']').remove();
            var newCompareModels= [];
            $.each(this.compareModels, function(i, n){
                var modelInfo = n.split('||');
                if (modelInfo[0] != mid) {
                    newCompareModels.push(this);
                }
            });
            this.compareModels = newCompareModels;
            $.cookie('compareModels',newCompareModels.join('##'),this.param);
            $('#compareModelCount').text(this.compareModels.length);
            this.updateUI();
            $("#compareModelChooser1_" + mid).attr("checked", false);
            $("#compareModelChooser2_" + mid).attr("checked", false);
        };    this.clickCompareModel = function (chooser, mid, mname, mthumb) {
            var checked = $("#" + chooser).attr("checked");
            if(checked) {
                if(this.addCompareModel(mid, mname, mthumb)) {
                    $("#compareModelChooser1_" + mid).attr("checked", true);
                    $("#compareModelChooser2_" + mid).attr("checked", true);
                } else {
                    $("#compareModelChooser1_" + mid).attr("checked", false);
                    $("#compareModelChooser2_" + mid).attr("checked", false);
                }
            } else {
                this.deleteCompareModel(mid);
            }
        };
        this.resetCompareModel = function (){
            $.each(this.compareModels, function(i, n){
                var modelInfo = n.split('||');
                $("#compareModelChooser1_" + modelInfo[0]).attr("checked", false);
                $("#compareModelChooser2_" + modelInfo[0]).attr("checked", false);
            });
            $('#proBoxUl>li').not('.template').remove();
            $.cookie('compareModels','',this.param);
            this.compareModels = [];
            $('#compareModelCount').text(this.compareModels.length);
            this.updateUI();
        };
        this.compareIt = function (){
            if (this.compareModels.length < 2) {
                alert('最少要选择2款车型进行比较 :-)');
                return false;
            }
            var mids = [];
            $.each(this.compareModels, function(i, n){
                var modelInfo = n.split('||');
                mids.push(modelInfo[0]);
            });
    //        mids.reverse();
            window.open('http://price.pcauto.com.cn/compare_config.jsp?type=2&mid='+mids.join(','));
            return true;
        };
    }