欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js代码实现评分功能

程序员文章站 2022-10-30 18:13:29
js代码实现评分功能

js代码实现评分功能

<!doctype html>


<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        onload = function () {
            var clickindex = -1;
            var imgs = document.getelementsbytagname('img');
            for (var i = 0; i < imgs.length; i++) {
                imgs[i].id = i;
            }
            for (var i = 0; i < imgs.length; i++) {
                imgs[i].onmouseover = function () {
                    for (var i = 0; i < imgs.length; i++) {                        
                        if (imgs[i].id<=this.id) {
                            imgs[i].src = 'star2.png';
                        }
                        else{
                            imgs[i].src = 'star1.png';
                        }
                    }
                   
                };


                imgs[i].onmouseleave = function () {
                    for (var i = 0; i < imgs.length; i++) {
                        if (i<=clickindex) {
                            imgs[i].src = 'star2.png';
                        } else {
                            imgs[i].src = 'star1.png';
                        }                                                 
                    }
                };


                imgs[i].onclick = function () {
                    for (var i = 0; i < imgs.length; i++) {
                        clickindex = this.id;
                    }
                };
            }
        }
    </script>
</head>
<body>
    <img src="star1.png" />
    <img src="star1.png" />
    <img src="star1.png" />
    <img src="star1.png" />
    <img src="star1.png" />
</body>
</html>