<!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">
// 补充代码
window.onload()=function()
{
var name=document.getElementsByTagName("input")[0];
var word=document.getElementsByTagName("span")[0];
name.onfocus=function()
{
if(this.value="")
{
this.innerHTML="请输入你的名字";
}
}
word.onblur=function()
{
if(this.value=="")
{
this.innerHTML="用户名不能为空";
this.className="red";
}
else
{
this.innerHTML="";
}
}
}
</script>
</body>
</html>
奋斗好青年
相关分类