evastar.js
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Evaluate Star
* Code in 2014/11/10
* By van
*/
define( 'evastar' ,function( require, exports, module ){
var evastar = function( target, options ){
this.target = target;
this.o = {
area: 'eva-stars-click',
starElement: '<i></i>',
showtag: "<span></span>",
inputname: 'ihidden',
number: 5,
recode: ["一星","二星","三星","四星","五星"],
closeStar: 'grey',
openStar: 'yellow',
callback: function(){}
}
this.o = $.extend( this.o, options );
this.init();
};
evastar.prototype = {
init: function(){
this.makeHtml();
this.starListener();
},
//对于指定评价星星区域添加所需html内容
makeHtml: function(){
var $_this = $(this.target);
if ( $_this.length > 0 ){
for ( var i=0;i < this.o.number;i++ ){
var star = $(this.o.starElement);
star.attr('title', this.o.recode[i]);
star.attr('class', "targetstar");
star.appendTo( $(this.target) );
}
$(this.o.input).appendTo( $_this );
$('<input type="hidden">').addClass(this.o.inputname).appendTo( $_this );
$(this.o.showtag).insertAfter( $_this );
}
},
//评价星星事件监听集合
starListener: function(){
var _this = this,
result = 0;
$(this.target).find( /\w+/.exec(this.o.starElement)[0] )
.bind({
mouseenter: function(){
_this.change( $(this).index()+1 );
},
mouseleave: function(){
_this.change( result );
},
click: function(){
$(this).parent().next().html( $(this).attr("title") );
result = $(this).index()+1;
_this.change( result );
_this.record( result )
_this.o.callback() && 1;
}
});
},
//依据点击的星星标签索引值改变选中状态。
change: function( n ){
$(this.target).attr("class", this.o.area).addClass( "stars"+(n) );
},
//将点击的评分录入隐藏input标签中。
record: function( n ){
$(this.target).find( "."+this.o.inputname ).attr( "value",n );
}
};
return evastar;
});