新type屬性介紹

HTML5中的type.png
其中標(biāo)有`紅色5`的代表`HTML5`中推出的

input新type屬性.png
- 新type屬性的注意要點(diǎn)
* 點(diǎn)擊不同type的input標(biāo)簽會(huì)有不一樣的彈出內(nèi)容
* 如果發(fā)現(xiàn)w3cschool內(nèi)容不全,建議去MDN搜索
* 并不是每一個(gè)新type屬性,在PC端都有不同的顯示
* color, date, number 這些效果較為明顯
相容性問(wèn)題- 由於ie的相容性的問(wèn)題,在不同的瀏覽器中顯示效果不盡相同
- 但是在行動(dòng)裝置上的支援效果較好,可以將該頁(yè)面?zhèn)魉偷绞謾C(jī)進(jìn)行測(cè)試
實(shí)際開(kāi)發(fā)中可以依照需求選用
input
表單驗(yàn)證
api
文件的查閱位置加如下
[w3cSchool_事件
屬性]w3School
[w3cSchool_input標(biāo)籤]w3cSchool
#[w3cSchool_input標(biāo)籤]w3cSchool #email標(biāo)籤
當(dāng)我們點(diǎn)擊
提交
按鈕
時(shí),如果輸入的
email
標(biāo)籤並不會(huì)驗(yàn)證內(nèi)容是否為空,這個(gè)需要注意
- 對(duì)於沒(méi)有自帶驗(yàn)證效果的標(biāo)籤,就需要手動(dòng)新增屬性增加驗(yàn)證了
required
屬性即可,不需要賦值

#範(fàn)例程式碼:
當(dāng)控制項(xiàng)
沒(méi)有輸入任何內(nèi)容直接點(diǎn)擊提交時(shí),會(huì)彈出提示
#在需要新增自訂驗(yàn)證規(guī)則的元素中新增
required
標(biāo)籤
###使用###正規(guī)表示式###編寫(xiě)驗(yàn)證規(guī)則##################範(fàn)例程式碼:##### #############當(dāng)我們輸入的內(nèi)容跟驗(yàn)證條件不符時(shí),就會(huì)彈出對(duì)應(yīng)的提示#################### #######自訂驗(yàn)證.png####
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="">
email:<input type="email" name="userEmail">
<br/>
tel:<input type="tel" required pattern="[0-9]{3}">
<br/>
<input type="submit">
</form>
</body>
</html>
自定義驗(yàn)證信息
系統(tǒng)的提示消息只能夠提示格式錯(cuò)誤,如果想要更為詳細(xì)的就需要我們通過(guò)js來(lái)自定義了

輸入中.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="">
email:<input type="email" name="userEmail">
<br/>
tel:<input type="tel" required pattern="[0-9]{3}" id="telInput">
<br/>
<input type="submit">
</form>
</body>
</html>
<script>
var telInput = document.getElementById("telInput");
// 正在輸入時(shí)
telInput.oninput=function () {
this.setCustomValidity("請(qǐng)正確輸入哦");
}
// 驗(yàn)證失敗時(shí)
telInput.oninvalid=function(){
this.setCustomValidity("請(qǐng)不要輸入火星的手機(jī)號(hào)好嗎?");
};
</script>
總結(jié)
以上是關(guān)於HTML5中input標(biāo)籤(type屬性)的詳細(xì)介紹的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!