HTML事件屬性onblur
HTML事件屬性onblur
觸發onblur
屬性當元素失去焦點。
HTML5中的新功能
沒有。
句法
<element onblur="script">
支持的標簽
所有HTML元素,EXCEPT:
<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, <title>
瀏覽器兼容性
onblur | Yes | Yes | Yes | Yes | Yes |
例子
<!DOCTYPE html>
<html>
<body>
Enter your first name: <input type="text" name="fname" id="fname" onblur="myFunction()">
Enter your last name: <input type="text" name="lname" id="lname" onblur="myFunction()">
<script>
function myFunction() {
console.log("lost focus");
}
</script>
</body>
</html>
Click to view the demo
實施例2
下面的代碼顯示了如何聽輸入密碼的Focus Lost事件。
<html>
<head>
<script type="text/javascript">
function checkRequired() {
document.write("value is required in field");
}
</script>
</head>
<body>
<form name="someForm">
<input type="text" name="text1" /><br />
<input type="password" name="text2" onblur="checkRequired();"/><br />
<input type="hidden" name="text3" value="hidden value" />
<textarea name="text4" cols=50 rows=10>The text area</textarea>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Click to view the demo