溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

html中onfocus和onblur是什么

發(fā)布時間:2020-09-26 15:15:01 來源:億速云 閱讀:493 作者:小新 欄目:web開發(fā)

小編給大家分享一下html中onfocus和onblur是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

1、onfocus事件屬性

onfocus 屬性在元素獲得焦點時被觸發(fā),即:獲得焦點事件;常用于 <input>、<select> 以及 <a>標簽中。

注:onfocus 屬性不適合用于以下元素中使用:<base>、<bdo>、<br>、<head>、<html>、<iframe>、<meta>、<param>、<script>、<style> 或 <title>。

2、onblur事件屬性

onblur 屬性在元素失去焦點時觸發(fā),即:失去焦點事件;常用于表單驗證代碼(例如用戶離開表單字段)中。

說明:

1、onfocus 屬性與 onblur 屬性是相反的。

2、所有主流瀏覽器都支持 onfocus 屬性和 onblur 屬性。

下面我們通過簡單的代碼示例來介紹onfocus 屬性和 onblur 屬性的用法。

html onfocus 事件屬性的用法

代碼示例:當輸入字段獲得焦點時觸發(fā)函數(shù)。此函數(shù)改變輸入字段的背景色(黃色)

<script>
function setStyle(x){
   document.getElementById(x).style.background="yellow";
}
</script>

<div>
   <p>當輸入字段獲得焦點時觸發(fā)函數(shù)。此函數(shù)改變輸入字段的背景色。</p>
   用戶名: <input type="text" id="fname" onfocus="setStyle(this.id)"><br>
   密&nbsp;&nbsp;&nbsp;碼: <input type="password" id="lname" onfocus="setStyle(this.id)">
</div>

效果圖:

html中onfocus和onblur是什么

html onblur 事件屬性的用法

代碼示例:當用戶離開輸入字段時對其進行驗證:小寫字母轉(zhuǎn)大寫

<script>
function upperCase(){
   var x=document.getElementById("fname").value
   document.getElementById("fname").value=x.toUpperCase()
}
</script>
<div>
	<p>請輸入您的姓名,然后把焦點移動到字段外:</p>
        用戶名(英文字符):<input type="text" name="fname" id="fname" onblur="upperCase()">
</div>

效果圖:

html中onfocus和onblur是什么

看完了這篇文章,相信你對html中onfocus和onblur是什么有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI