onfocus 屬性在元素獲得焦點時觸發。
onfocus 常用於<input>、<select> 以及<a>.
提示:onfocus 屬性與onblur 屬性相反。
註:onfocus 屬性不適用於下列元素:<base>、<bdo>、<br>、<head>、<html>、<iframe>、<meta>、<param>、<script>、<style> 或<title>。
實例當輸入欄位獲得焦點時觸發函數。此函數改變輸入欄位的背景色
<script>function setStyle(x){document.getElementById(x).style.background=yellow;}</script></head><body> <p>當輸入欄位獲得焦點時觸發函數。此函數會改變輸入欄位的背景色。 </p> First name: <input type=text id=fname onfocus=setStyle(this.id)><br>Last name: <input type=text id=lname onfocus=setStyle(this.id)> </body >HTML onblur 事件屬性定義和用法
onblur 屬性在元素失去焦點時觸發。
onblur 常用於表單驗證程式碼(例如使用者離開表單欄位)。
實例當使用者離開輸入欄位時對其進行驗證:
<script>function upperCase(){var x=document.getElementById(fname).valuedocument.getElementById(fname).value=x.toUpperCase()}</script></head><body> <p>請輸入您的姓名,然後把焦點移到欄位外:</p>請輸入您的姓名(英文字元):<input type=text name=fname id=fname onblur=upperCase()> </body>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。