麻烦老师帮忙检查并且优化一下代码,谢谢~

来源:3-8 编程练习

慕粉2243585596

2020-05-15 19:38:46

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>onblur和onfocus</title>
    <style type="text/css">
        .red{
     border:1px solid red;
     }
    </style>
</head>
<body>
姓名:<input type="text"> <span></span>
<script type="text/javascript">
// 补充代码
var inputdom=document.getElementsByTagName("input")[0];
var spandom=document.getElementsByTagName("span")[0];
inputdom.onfocus=function(){
    this.className="";
    spandom.innerHTML="请输入您的姓名";
    
}
inputdom.onblur=function(){
    if(this.value==""){
        spandom.innerHTML="用户名不能为空";
        this.className="red"; 
    }else{
        spandom.innerHTML="";
    }
}
</script>
</body>
</html>


写回答

1回答

好帮手慕慕子

2020-05-16

同学你好,效果实现的很棒,代码也很简洁,不用再优化了,继续加油,祝学习愉快~

0

0 学习 · 40143 问题

查看课程