html中onfocus和onblur是什么

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

我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、臺(tái)州ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的臺(tái)州網(wǎng)站制作公司

1、onfocus事件屬性

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

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

2、onblur事件屬性

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

說明:

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

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

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

html onfocus 事件屬性的用法

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

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

<div>
   <p>當(dāng)輸入字段獲得焦點(diǎn)時(shí)觸發(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 事件屬性的用法

代碼示例:當(dāng)用戶離開輸入字段時(shí)對其進(jìn)行驗(yàn)證:小寫字母轉(zhuǎn)大寫

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

效果圖:

html中onfocus和onblur是什么

看完了這篇文章,相信你對html中onfocus和onblur是什么有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前題目:html中onfocus和onblur是什么
網(wǎng)頁URL:http://muchs.cn/article24/ipieje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、外貿(mào)網(wǎng)站建設(shè)Google、ChatGPT網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)