4-11 Programming exercises (need to be optimized) – learn onblur and onfocus mouse events, based on the renderings, add code
Topic request
Thought analysis
Code implementation
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>practice</title>
<style>
.red {
border: 1px solid red;
}
.black {
border: 1px solid # 000;
}
</style>
</head>
<body>Name:<input type="text" id="text"><span></span>
<script>
var text = document.getElementById('text');
var tip = document.getElementsByTagName('span') [0];
text.onfocus = function () {
if (text.value.length == 0) {
tip.innerText = 'Please enter your name';
}
}
text.onblur = function () {
if (text.value.length == 0) {
tip.innerText = 'User name cannot be empty';
text.className = 'red';
} else {
tip.innerText = ' ';
text.className = 'black'; }}</script>
</body>
</html>
Copy the code
rendering
defects
Instead: added the class property black to do this