新手​实现五星评价问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
 .star-off {
            height: 19px;
            width: 19px;
            display: inline-block;
            background: url("images/stare.png") no-repeat;
            background-position: -39px 0;
        }

        .star-on {
            height: 19px;
            width: 19px;
            display: inline-block;
            background: url("images/stare.png") no-repeat;
            background-position: -2px 0;
        }
    </style>
    <script>
 window.onload = function () {
            var aB = document.getElementsByTagName('b');
            for (var i = 0; i < aB.length; i++) {
                aB[i].onmouseover = function () {
                    aB[i].className = 'star-on';
                }
            }
        }
    </script>
</head>
<body>
<div id="commit">
    <span>总体评价:</span>
    <span class="star">
            <b class="star-off"></b>
            <b class="star-off"></b>
            <b class="star-off"></b>
            <b class="star-off"></b>
            <b class="star-off"></b>

        </span>
    <span id="text"></span>
</div>
</body>
</html>

请问28行为什么报错啊

慕九州7009252
浏览 890回答 1
1回答

冥oo冥

在function函数里不识别aB[i],改用this吧var aB = document.getElementsByTagName('b');            for (var i = 0; i < aB.length; i++) {                aB[i].onmouseover = function () {                    this.className = 'star-on';                }            }简单的说,for(var i=0;i<10;i++){ a.onmouseover = b;}function b(){ //这儿不识别i}
打开App,查看更多内容
随时随地看视频慕课网APP