溫馨提示×

溫馨提示×

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

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

如何完成HTML5注冊表單的自動(dòng)聚焦與占位文本

發(fā)布時(shí)間:2021-09-14 17:42:58 來源:億速云 閱讀:109 作者:柒染 欄目:web開發(fā)

這篇文章給大家介紹如何完成HTML5注冊表單的自動(dòng)聚焦與占位文本,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

首先看下面要使用HTML自動(dòng)聚焦和占位文本的示例代碼

代碼如下:

<!DOCTYPE html> 2: <html> 
<head> 
<title>注冊帳號(hào)</title> 
<meta charset="utf-8"> 
</head> 
<body> 
<form method="post" action="goto"> 
<fieldset id="register_information"> 
<legend>新用戶注冊</legend> 
<ol> 
<li> 
<label for="name">郵 箱</label> 
<input type="email" id="name" name="name"> 
</li> 
<li> 
<label for="user"> 用戶名</label> 
<input type="text" id="user" name="user"> 
</li> 
<li> 
<label for="nickname"> 顯示名稱</label> 
<input type="text" id="nickname" name="user"> 
</li> 
<li> 
<label for="password">密碼</label> 
<input type="password" id="password" name="user_password"> 
</li> 
<li> 
<label for="confirm_password">確認(rèn)密碼</label> 
<input type="password" id="confirm_password" name="user_password"> 
</li> 
</ol> 
</fieldset> 
</form> 
</body> 
</html>


使用自動(dòng)聚焦
要使用HTML5的自動(dòng)聚焦功能,只需要在表單域中添加autofocus屬性即可
例如上面,想讓頁面加載完成時(shí)自動(dòng)將光標(biāo)定位到表單的第一個(gè)表單域郵箱上以及提高輸入效率。

代碼如下:

<li> 
<label for="name">郵 箱</label> 
<input type="email" id="name" name="name" autofocus> 
</li>

如何完成HTML5注冊表單的自動(dòng)聚焦與占位文本
需要注意的是,如果頁面中設(shè)置了多個(gè)autofocus屬性,那么用戶的光標(biāo)只會(huì)定位到最后一個(gè)設(shè)置了autofocus屬性的表單域上。
使用占位文本
占位文本的最大用處,就是向用戶說明應(yīng)該如何正確的填寫表單。即進(jìn)行輸入提示。要使用占位文本的話,只需要在輸入域中添加placeholder屬性即可
下面是使用了placeholder屬性的輸入表單域

代碼如下:

<ol> 
<li> 
<label for="name">郵 箱</label> 
<input type="email" id="name" name="name" autofocus placeholder="請輸入有效的郵件地址"> 
</li> 
<li> 
<label for="user"> 用戶名</label> 
<input type="text" id="user" name="user" placeholder="輸入用戶名"> 
</li> 
<li> 
<label for="nickname"> 顯示名稱</label> 
<input type="text" id="nickname" name="user" placeholder="輸入昵稱"> 
</li> 
<li> 
<label for="password">密碼</label> 
<input type="password" id="password" name="user_password" placeholder="輸入密碼"> 
</li> 
<li> 
<label for="confirm_password">確認(rèn)密碼</label> 
<input type="password" id="confirm_password" name="user_password" placeholder="再次輸入密碼"> 
</li> 
</ol>

運(yùn)行效果如下
如何完成HTML5注冊表單的自動(dòng)聚焦與占位文本 
是否啟用自動(dòng)完成
在HTML5中引入了autocomplete屬性。用來通知瀏覽器是否為當(dāng)前表單域自動(dòng)填充數(shù)據(jù)。某些瀏覽器會(huì)記住用戶之前輸入的數(shù)據(jù),而在某些情況下,我們可能并不希望用戶使用記錄數(shù)據(jù),特別類似于密碼這一類的
關(guān)閉自動(dòng)完成

代碼如下:

<input type="password" id="password" name="user_password" autocomplete="off" placeholder="輸入密碼">

只需要將atuocomplete的值設(shè)置成off,就可以阻止自動(dòng)完成。

關(guān)于如何完成HTML5注冊表單的自動(dòng)聚焦與占位文本就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

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

AI