onblur 會在物件失去焦點時發生。它是表單互動中最常被使用的事件之一,典型應用包含離開輸入框時的即時格式轉換、即時驗證與自動補完。
onblur="SomeJavaScriptCode"
SomeJavaScriptCode 為必填,用來指定事件發生時要執行的 JavaScript。
<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <button>, <caption>, <cite>, <dd>, <del>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>, <form>, <frame>, <frameset>, <h1> 到 <h6>, <hr>, <i>, <iframe>, <img>, <input>, <ins>, <kbd>, <label>, <legend>, <li>, <object>, <ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>
button, checkbox, fileUpload, layer, frame, password, radio, reset, submit, text, textarea, window
<html>
<head>
<script type="text/javascript">
function upperCase()
{
var x = document.getElementById("fname").value
document.getElementById("fname").value = x.toUpperCase()
}
</script>
</head>
<body>
輸入您的姓名:
<input type="text" id="fname" onblur="upperCase()" />
</body>
</html>
當游標離開輸入框的瞬間,內容就會被轉成大寫。
實務上 onblur 常與 onfocus 成對出現:例如輸入框內顯示提示文字,取得焦點時清空、失去焦點時若為空則還原提示,這是早期表單很常見的寫法。若使用 HTML5,直接改用 placeholder 屬性會更省事。