溫馨提示×

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

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

HTML5中增強(qiáng)的Form表單元素都有哪些

發(fā)布時(shí)間:2021-10-08 14:42:30 來源:億速云 閱讀:156 作者:柒染 欄目:web開發(fā)

HTML5中增強(qiáng)的Form表單元素都有哪些,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

在HTML5增強(qiáng)的元素中,最值得關(guān)注的就是表單元素。在HTML5中,表單已經(jīng)做了重大的修整,一些以前需要通過JavaScript編碼實(shí)現(xiàn)的功能現(xiàn)在無需編碼就可輕松實(shí)現(xiàn)。在開始討論之前,需要注意一點(diǎn):

在HTML5中,表單控件是可以處于其所屬的一個(gè)或多個(gè)表單的外部的。所以,表單控件像fieldset,label,input這些都加入了form屬性,用于標(biāo)識(shí)表單控件所屬的表單。

在HTML5中

1. form元素自身增加了兩個(gè)新的屬性:autocomplete和novalidate。autocomplete屬性用于啟用“下拉建議列表”功能,novalidate屬性用于關(guān)閉表單驗(yàn)證功能,這在測(cè)試時(shí)會(huì)很有用。

2. fieldset元素增加了三個(gè)新屬性:disable、name和form。disable屬性用于禁用fieldset,name屬性用于設(shè)置fieldset的名稱,form屬性的值是fieldset所屬的一個(gè)或多個(gè)表單的ID,這個(gè)前面也說了,當(dāng)fieldset被置于表單的外部時(shí),你必須設(shè)置該fieldset標(biāo)簽的form屬性,這樣fieldset就可以正確地與一個(gè)或多個(gè)表單關(guān)聯(lián)起來。

3. label元素除for屬性外,只增加了form屬性。這里值得一提的是for屬性,我以前還真沒注意過。for屬性用于指定label附屬的表單控件,這樣點(diǎn)擊這個(gè)label時(shí)會(huì)讓附屬的表單控件獲得焦點(diǎn),比如:

代碼如下:


<form action="demo_form.asp" id="form1">
 <label for="name">Click Me</label><input id="name" type="text"></input>
 <input type="submit" value="Submit" />
</form>


點(diǎn)擊"Click Me",則后面的輸入框會(huì)獲得焦點(diǎn)。

4. input元素引入了一些新的類型與屬性,增強(qiáng)了表單的可用性。這些新的輸入類型,用于對(duì)數(shù)據(jù)進(jìn)行組織和歸類,非常有用,遺憾的是并沒有哪一個(gè)瀏覽器能很好的支持所有的這些類型。
除了原來button,text,submit,checkbox,radio,select,password的類型,HTML5加入了下列新的input類型:

顏色:color
各種日期:date, datetime, datetime-local, month, week, time
電子郵件:email
數(shù)字:number
范圍:range
搜索:search
電話:tel
URL類型:url

可以運(yùn)行下面的例子來查看不同瀏覽器的支持情況:

代碼如下:


<form action="demo_form.asp">
 Select your favorite color: <input type="color" name="favcolor" />
 Birthday: <input type="date" name="bday" />
 Birthday (date and time): <input type="datetime" name="bdaytime" />
 Birthday (date and time): <input type="datetime-local" name="bdaytime" />
 Birthday (month and year): <input type="month" name="bdaymonth" />
 Select a time: <input type="time" name="usr_time" />
 Select a week: <input type="week" name="week_year" />
 Quantity (between 1 and 5): <input type="number" name="quantity" min="1" max="5" />
 Quantity(between 1 and 10): <input type="range" name="points" min="1" max="10" />
 Search Google: <input type="search" name="googlesearch" />
 Telephone: <input type="tel" name="usrtel" />
 Add your homepage: <input type="url" name="homepage" />
 E-mail: <input type="email" name="usremail" />
 <input src="submitbutton.png" type="submit" />
</form>


下面這些是新增加的input屬性:
autocomplete:自動(dòng)顯示以前輸入過的信息,取值"on"或者"off"。適用于text, search, url, tel, email, password, datepickers, range, and color類型。

autofocus:頁(yè)面加載完成后自動(dòng)獲取到焦點(diǎn)。
form:指定input所屬的form,可以是多個(gè)。
formaction:指定form提交后處理這個(gè)input的頁(yè)面(URL)或文件。  
formenctype:指定form提交后數(shù)據(jù)如何編碼。
formmethod:指定發(fā)送form數(shù)據(jù)的HTTP方法,會(huì)覆蓋相應(yīng)form的HTTP方法。
formnovalidate:提交前不檢查數(shù)據(jù)的有效性。
formtarget:指定在那個(gè)地方顯示form提交后response的內(nèi)容。
height, width:輸入框長(zhǎng)和寬,只適用于image類型?!?br/>max,min:輸入值的最大值和最小值。適用于有意義的number,range, 日期類型。  
multiple:是否允許輸入多個(gè)值,適用于email和file類型。
pattern:指定驗(yàn)證輸入值的正則表達(dá)式,適用于text,search,url,tel,email,password。
placeholder:輸入前的提示信息,適用于text,search,url,tel,email,password。
required:是否是必填項(xiàng),如果不填必填項(xiàng),則表單不能提交,適用于text, search, url, tel, email, password, date pickers, number, checkbox, radio, 和file類型。
step:輸入自動(dòng)增長(zhǎng)時(shí)的步長(zhǎng)值,適用于number, range, date, datetime, datetime-local, month, time和week類型。
list:輸入項(xiàng)的候選列表,需要和datalist元素配合使用,list屬性可用在這些類型上:text、search、url、tel、email、date、number、range和color,目測(cè)在FireFox上有效??匆粋€(gè)小例子:

代碼如下:


<fieldset>
 <legend> Favorites </legend>
 <p>
   <label>
     <input type="text" name="favorites" list="options">
     <datalist id="options">
       <option value="A">
       <option value="B">
       <option value="C">
     </datalist>
   </label>
 </p>
</fieldset>


下面的例子嘗試使用了各個(gè)屬性,可以運(yùn)行在不同的瀏覽器下查看實(shí)際效果:

代碼如下:


<form action="demo_form.asp">
 E-mail: <input type="email" name="email" autocomplete="on" />
 Image: <input type="image" src="img_submit.gif" alt="Submit" width="48" height="48"/>
 Enter a date before 1980-01-01:<input type="date" name="bday" max="1979-12-31">
 Enter a date after 2000-01-01:<input type="date" name="bday" min="2000-01-02">
 Quantity (between 1 and 5):<input type="number" name="quantity" min="1" max="5" />
 Select images: <input type="file" name="img" multiple="multiple" />
 Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}" title="Three letter country code" />
 First Name: <input type="text" name="fname" placeholder="First name" />
 Username: <input type="text" name="usrname" required="required" />
 Number: <input type="number" name="points" step="3" />

 <input type="submit" />
 <input type="submit" formaction="demo_admin.asp" value="Submit as admin" />
 <input type="submit" formenctype="multipart/form-data" value="Submit as Multipart/form-data" />
 <input type="submit" formmethod="post" formaction="demo_post.asp" value="Submit using POST" />
 <input type="submit" formnovalidate="formnovalidate" value="Submit without validation" />
 <input type="submit" formtarget="_blank" value="Submit to a new window" />
</form>
<form action="demo_form.asp" id="form1">
 First name: <input type="text" name="fname" />
<input type="submit" value="Submit" />
</form>
 Last name: <input type="text" name="lname" form="form1" />


雖然并不是所有的瀏覽器都支持全部的類型,但是還是鼓勵(lì)大家使用這些新類型,因?yàn)榧词篂g覽器不支持,只不過是會(huì)退化成簡(jiǎn)單的text輸入框而已。

關(guān)于HTML5中增強(qiáng)的Form表單元素都有哪些問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

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

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

AI