JS star rating

Source: Internet
Author: User

Note:

This method uses the Jquery library and is currently compatible with IE8. The stars in the two colors in this example are all placed in a png image. Of course there are other implementation ideas. This example shows that the current page has only one star rating.

Ideas:

The number of stars is displayed by listening to three events: mouseenter, mouseout, and click. I used two variables (temp_value, choice_value) to store the currently selected star value and the current temporary star value;

  • Temp_value: the temporary star value;
  • Choice_value: The selected star value;
  • Mouseenter: move the mouse in to obtain the current star level and assign temp_value to adjust the color of the current star image;
  • Mouseout: remove the mouse. Compare the choice_value to adjust the color of the star image;
  • Click: After clicking the mouse, obtain the current star value and assign it to choice_value. (Note that the mouseenter event has been performed before clicking and the mouseout event has been performed after clicking)
  • After selecting a value, you can use JS to obtain the value of the choice_value variable when you need to submit the evaluation value;

    HTML Structure Code:

    Rating:
       
       

    Note: if there is more than one star rating on the page, that is, multiple rows, you only need to modify the li id in the Dom and add dynamic subscript, to distinguish different elements. Another point is that I put the selected star value in the JS variable. When there are multiple rating values, we recommend that you add a hidden form field to the Dom to store the selected star value.

    JS implementation code:

    <Script src = "http://code.jquery.com/jquery-latest.js"> </script> <script type = "text/javascript"> $ (document ). ready (function () {var _ temp_value = 0, // The level value choiceValue = 0 when the mouse hover; // The default level value is 1 // $ ("# star li") when the mouse is moved in "). mouseenter (function () {// get the current star _ temp_value = new Number ($ (this ). attr ("star"); showStar (); // when the mouse is moved in }). mouseout (function () {hideStar (); // when the mouse is moved in }). click (function () {// click to set the selected value choiceValue = _ temp_value;
    HideSatr () ;}); // star display function showStar () {for (var I = 1; I <_ temp_value + 1; ++ I) {$ ("# star" + I ). addClass ("choice") ;}} function hideStar () {for (var I = 5; I> choiceValue; -- I) {$ ("# star" + I ). removeClass ("choice") ;}}); </script>

    If you have any questions, please contact me! Here is the Demo address: http://xiaoweijs.duapp.com/demo/star-level/index.html

    Author: Ziv Serena
    Source: http://www.cnblogs.com/zivxiaowei/
    About the author: programmers who focus on Java Technology keep a high interest in JS development. Like music, reading, FM, etc.
    The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide a connection to the original article on the article page.
    If there is a problem, you can mail: wewoor@foxmail.com
    Weibo: Ziv Xiaowei

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.