老师帮忙看下

来源:2-7 编程练习

2022一定转行成功

2021-03-04 21:22:11

相关代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML事件</title>
</head>
<body>
<!--补充代码-->
<button colorin="red">开始</button>
<button color="purple">结束</button>
<script type="text/javascript">
//补充代码
var begin=document.getElementsByTagName("button")[0];
var over=document.getElementsByTagName("button")[1];
function in(){
this.style.color=this.getAttribute("colorin");
}
begin.onclick=in;
</script>
</body>
</html>


写回答

1回答

好帮手慕言

2021-03-05

同学你好,代码存在以下问题:

1、in是js中的关键字,不能用来做函数名字,建议:修改函数名,如下:

http://img.mukewang.com/climg/60418e2f09d52ff203430076.jpg

http://img.mukewang.com/climg/60418e380908b06603310085.jpg

2、同学使用的是点击事件,按照要求,是要使用移入移出事件,修改如下:
http://img.mukewang.com/climg/60418f0809d5fbab04750120.jpg

http://img.mukewang.com/climg/60418f1009b295ac04150106.jpg

3、也需要给“结束”按钮绑定事件,如下:

http://img.mukewang.com/climg/60418f5f09d3628f03800087.jpg

属性名可以和“开始”按钮的保持一致,move函数两个按钮都可以使用了:

http://img.mukewang.com/climg/60418f7e097f291d07360107.jpg

修改后的代码如下:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>HTML事件</title>
</head>

<body>
    <!--补充代码-->
    <button colorin="red">开始</button>
    <!-- <button color="purple">结束</button> -->
    <button colorin="purple">结束</button>
    <script type="text/javascript">
        //补充代码
        var begin = document.getElementsByTagName("button")[0];
        var over = document.getElementsByTagName("button")[1];
        // function in (){
        function move() {
            this.style.color = this.getAttribute("colorin");
        }
        function moveOut() {
            this.style.color = "gray"
        }
        // begin.onclick =in;
        begin.onmouseover = move;
        begin.onmouseout = moveOut;

        over.onmouseover = move;
        over.onmouseout = moveOut;
    </script>
</body>

</html>

祝学习愉快~

0

0 学习 · 40143 问题

查看课程