Document directory
- Rating. js code:
- Page code:
Functions:
- Multiple stars can be used on the same page without interfering with each other.
- The number of stars can be customized.
- You can set the default star rating.
- After selecting a star, there is still a hover Effect
- Customizable star rating description
Rating. js code:
Function initstar (count, cur, S0, S1, CTN, txt) {for (VAR I = 1; I <= count; I ++) {var P = (I <= cur )? S1: S0; $ (" "). ATTR ({"src": P, "flag": I }).css ("cursor", "Pointer "). mouseover (function () {var cur = parseint ($ (this ). ATTR ("flag"); $ ("#" + CTN ). children (). each (function () {var I = parseint ($ (this ). ATTR ("flag"); var P = (I <= cur )? S1: S0; $ (this ). ATTR ("src", P) ;}); showstar (txt, cur );}). click (function () {// original blog: blog.csdn.net/bluceyoung $ ("#" + CTN ). ATTR ("Star", $ (this ). ATTR ("flag "));}). appendto ($ ("#" + CTN);} $ ("#" + CTN ). ATTR ("Star", cur ). mouseout (function () {var cur = parseint ($ (this ). ATTR ("star"); $ (this ). children (). each (function () {var P = ($ (this ). ATTR ("flag") <= cur )? S1: S0; $ (this ). ATTR ("src", P) ;}); showstar (txt, cur) ;});} function showstar (txt, cur) {$ ("#" + txt ). val (cur); $ ("#" + txt ). change ();}
Parameter description:
Count: Total number of stars
Cur: the number
S0: Image path of empty stars
S1: Image path of real stars
CTN: Create a Star Container
TXT: textbox that displays the rating description
Page code:
<HTML> <SCRIPT src = "http://code.jquery.com/jquery-1.8.2.min.js" type = "text/JavaScript"> </SCRIPT> <SCRIPT src = "rating. JS "type =" text/JavaScript "> </SCRIPT> <SCRIPT type =" text/JavaScript ">$ (document ). ready (function () {// create star initstar(5,3,'k.gif ', 's.gif', 'lblstar1', 'txt ') // rewrite the star description $ ("# TXT "). change (function () {var v = $ (this ). val (); Switch (v) {Case "1": V = "too bad"; break; Case "2": V = "to be improved"; break; case "3": V = "general"; break; Case "4": V = "good"; break; Case "5": V = "great "; break; default: V = "" ;}$ (this ). val (v );});}); </SCRIPT> <body> <label id = "lblstar1"> </label> <input type = "text" id = "TXT"> <br> </body>
By default, only the number of rating levels is displayed in the textbox of the rating description. to customize the rating description, add a change event and replace the value in it.
: