1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

评分效果

本帖由 不学无术2005-11-10 发布。版面名称:前端开发

  1. 不学无术

    不学无术 Ulysses 的元神

    注册:
    2005-08-31
    帖子:
    16,714
    赞:
    39
    演示
    先看演示:http://www.purewhite.cn/demo/rating/rating.html

    对应的 JavaScript 代码
    rating.js
    代码:
    function Rating(name,input){
    	this.name = name;
    	this.ratingMaxNum = 5;
    	this.selectedRating = 0;
    	this.onImageSrc = "images/rating/stars_on.gif";
    	this.offImageSrc = "images/rating/stars_off.gif";
    	document.write('<div id="ratingDiv"></div>');
    	document.write('<input name="' +input+ '" type="hidden" id=" '+input+'" value="0" />');
    	this.divElement = document.getElementById("ratingDiv");
    	this.inputElement = document.getElementById(input);
    	this.init();
    }
    
    Rating.prototype.init = function(){
    	var output = "";
    	for(i=1;i<=this.ratingMaxNum;i++){
    		output +=  '<img id="rating_'+i+'" name="rating_'+i+'" onClick="'+this.name+'.setRating('+i+');" onMouseOver="'+this.name+'.setOverState('+i+');" onMouseOut="'+this.name+'.setOutState('+i+');" src="'+this.offImageSrc+'" />';
    	}
    	this.divElement.innerHTML = output;
    }
    
    Rating.prototype.setRating = function(id){
    	if(this.selectedRating == id){
    		this.selectedRating = 0;
    	}else{
    		this.selectedRating = id;
    	}
    	this.refreshRating();
    	this.inputElement.value = id;
    }
    
    Rating.prototype.setOverState = function(id){
    	for(i=1;i<=this.ratingMaxNum;i++){
    		var imgElement = document.getElementById("rating_"+i);
    		if(i<=id){
    			imgElement.src = this.onImageSrc;
    
    		}else{
    			imgElement.src = this.offImageSrc;
    		}
    	}
    }
    
    Rating.prototype.setOutState = function(){
    
    	this.refreshRating();
    
    }
    
    Rating.prototype.refreshRating = function(){
    	for(i=1;i<=this.ratingMaxNum;i++){
    		var imgElement = document.getElementById("rating_"+i);
    		if(i<=this.selectedRating){
    			imgElement.src = this.onImageSrc;
    		}else{
    			imgElement.src = this.offImageSrc;
    		}
    	}
    }
    行间代码,放置于显示区域
    代码:
    <script language="javascript">
    	rt = new Rating("rt","icon");
    	rt.setRating(2);
    </script>
    来源及说明
    http://www.klstudio.com/blog/content.cfm?id=192
     
    #1 不学无术, 2005-11-10
    最后编辑: 2005-11-10
  2. 老林

    老林 New Member

    注册:
    2005-09-06
    帖子:
    10,580
    赞:
    36
    怎么提交?
     
  3. 不学无术

    不学无术 Ulysses 的元神

    注册:
    2005-08-31
    帖子:
    16,714
    赞:
    39
    用 AJAX 提交。

    :)
     
  4. kamsang

    kamsang New Member

    注册:
    2005-10-05
    帖子:
    9,080
    赞:
    58
    好咚咚!
     
  5. 老林

    老林 New Member

    注册:
    2005-09-06
    帖子:
    10,580
    赞:
    36
    不会用..
     
  6. 一路走

    一路走 New Member

    注册:
    2005-11-05
    帖子:
    2,933
    赞:
    18
    不会用。。。