代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
form{
width: 1000px;
max-width: 640px;
min-width: 320px;
margin:0 auto;
font-size:20px
}
input{
display: block;
height: 30px;
font-size: 20px;
width: 100%;
margin: 10px 0;
}
</style>
</head>
<body>
<form action="">
<fieldset>
<legend>表单事件</legend>
<label for="">
用户名:<input type="email" name="username" id="txt1"/>
</label>
<label for="">
密码:<input type="text" name="password" id="txt2"/>
</label>
<input type="submit" />
</fieldset>
</form>
<script>
//表单事件:oninout 当用户输入的时候
//oninvalid 当验证不通过的时候触发
var text1=document.getElementById('txt1');
var text2=document.getElementById('txt2');
var num=0;
text1.οninput=function(){
num++;
//将字数显示在2中
txt2.value=num;
}
//oninvalid 一般用于设置验证不通过时的提示文字
text1.oninvalid=function(){
//用于设置验证不通过时的提示文字
this.setCustomValidity('会输入邮箱吗')
}
</script>
</body>
</html>