input元素的23種type類型

隨着HTML5的出現,input元素新增了多種類型,用以接受各種類型的用戶輸入。其中,button、checkbox、file、hidden、image、password、radio、reset、submit、text這10個是傳統的輸入控件,新增的有color、date、datetime、datetime-local、email、month、number、range、search、tel、time、url、week共13個

傳統類型

text  定義單行的輸入字段,用戶可在其中輸入文本

password  定義密碼字段。該字段中的字符被掩碼

file  定義輸入字段和 "瀏覽"按鈕,供文件上傳

radio  定義單選按鈕

checkbox  定義複選框

hidden  定義隱藏的輸入字段

button  定義可點擊按鈕(多數情況下,用於通過JavaScript啓動腳本)

image  定義圖像形式的提交按鈕

reset  定義重置按鈕。重置按鈕會清除表單中的所有數據

submit  定義提交按鈕。提交按鈕會把表單數據發送到服務器

text

type="text"表示一個文本輸入框,它是默認的輸入類型,是一個單行的控件,一般是一個帶有內嵌框的矩形

password

type="password"表示一個密碼輸入框,它與文本輸入框幾乎一模一樣,功能上唯一的不同的字母輸入後會被隱藏,一般是一連串的點

【默認樣式】

chrome/safari/opera
    padding: 1px 0px;
    border: 2px inset;
firefox
    padding: 2px;
    border-width: 1px;
ie
    padding: 2px 1px;
    border-width: 1px;

【默認寬高】

chrome
    height: 14px;
    width: 148px;
safari
    height: 15px;
    width: 148px;
firefox
    height: 17px;
    width: 137px;
IE9+
    height: 14px;
    width: 147px;
IE8-
    height: 16px;
    width: 149px;
專門建立的學習Q-q-u-n: 731771211,分享學習方法和需要注意的小細節,不停更新最新的教程和學習技巧
(從零基礎開始到前端項目實戰教程,學習工具,全棧開發學習路線以及規劃)

【重置樣式】

padding: 0;
border: 1px solid;

注意:IE6瀏覽器設置的type="text"或"password"的input元素的寬高爲包含padding和border的寬高

【tips】模擬密碼顯示隱藏的功能

說明:現在很多軟件在密碼框右側都有一個小眼睛,用於設置密碼的顯示和隱藏。通過更改input元素的type屬性得以實現

<style>
body{
    margin: 0;
    font-size: 16px;
}    
#show{
    padding: 0;
    border: 1px solid black;
    height: 20px;
    width: 200px;
    line-height: 20px;
}
#set{
    display: inline-block;
    height: 22px;
    background-color: rgba(0,0,0,0.5);
    color: white;
    line-height: 18px;
    margin-left: -72px;
    cursor: pointer;
}
</style>
</head>
<body>
<input id="show" type="password" maxlength="6">
<span id="set">顯示密碼</span>
<script>
set.onclick = function(){
    if(this.innerHTML == '顯示密碼'){
        this.innerHTML = '隱藏密碼';
        show.type="text";
    }else{
        this.innerHTML = '顯示密碼';
        show.type="password";
    }
}    
</script>

file

type="file"定義輸入字段和"瀏覽"按鈕,用於文件上傳

【重置樣式】

    padding: 0;
    border: 0;

【默認寬高】

chrome
    height: 21px;
    width: 238px;
safari
    height: 21px;
    width: 238px;
firefox
    height: 27px;
    width: 222px;
IE9+
    height: 21px;
    width: 220px;
IE8
    height: 16px;
    width: 214px;
IE7-
    height: 15px;
    width: 210px;

radio

type="radio"定義單選按鈕,允許用戶從給定數目的選擇中選一個選項。同一組按鈕,name值一定要一致

注意:radio類型的input元素無法設置padding和border(除IE10-瀏覽器以外)

【默認樣式】

chrome/safari/opera/firefox
    margin: 3px 3px 0 5px;
    box-sizing:border-box;
ie11
    margin: 3px 3px 3px 4px;
    box-sizing:border-box;
ie10-
    padding: 3px;
    box-sizing:border-box;

【默認寬高】

chrome/safari/IE
    height: 13px;
    width: 13px;
firefox
    height: 16px;
    width: 16px;

【重置樣式】

    padding: 0;
    margin: 0;
    border: 0;

checkbox

type="checkbox"定義多選按鈕,允許用戶在給定數目的選擇中選擇一個或多個選項。同一組的按鈕,name取值一定要一致

注意:checkbox類型的input元素無法設置padding和border(除IE10-瀏覽器以外)

【默認樣式】

專門建立的學習Q-q-u-n: 731771211,分享學習方法和需要注意的小細節,不停更新最新的教程和學習技巧
(從零基礎開始到前端項目實戰教程,學習工具,全棧開發學習路線以及規劃)
chrome/safari/opera/firefox/ie11
    margin: 3px 3px 3px 4px;
    box-sizing:border-box;
ie10-
    padding: 3px;
    box-sizing:border-box;

【默認寬高】

chrome/safari/IE
    height: 13px;
    width: 13px;
firefox
    height: 16px;
    width: 16px;

【重置樣式】

    padding: 0;
    margin: 0;
    border: 0;

type="radio"或"checkbox"的input元素支持checked屬性

hidden

type="hidden"定義隱藏輸入類型用於在表單中增加對用戶不可見,但需要提交的額外數據

注意:disabled屬性無法與type="hidden"的input元素一起使用

//點擊提交按鈕後,隱藏域的內容test=12會包含在URL中
<form name="form" action="#">
    <input type="hidden" name="test" value="12">
    <input type="submit">
</form>

button

type="button"的input輸入類型定義可點擊的按鈕,但沒有任何行爲,常用於在用戶點擊時啓動javascript程序

【button、submit、reset的默認樣式】

chrome/safari
    padding: 1px 6px;
    border: 2px outset buttonface;
    box-sizing:border-box;
firefox
    padding: 0 6px;
    border: 3px outset;
    box-sizing:border-box;
IE9+
    padding: 3px 10px;
    border: 1px outset;
    box-sizing:border-box;    
IE8
    padding: 3px 10px;
    border: 1px outset;
IE7-
    padding: 1px 0.5px;
    border: 1px outset;

注意:IE8-瀏覽器的box-sizing:content-box;而其他瀏覽器的box-sizing:border-box;

<input type="button" value="Click me" onclick="alert(1)" />    

type="button"的input輸入類型和button元素有很多重疊特性

image

type="image"的input輸入類型定義圖像形式的提交按鈕,該類型可以設置width、height、src、alt這四個屬性

用圖片作爲提交按鈕會一起發送點擊在圖片上的x和y座標,這樣可以與服務器端圖片地圖結合使用,如果圖片有name屬性,也會隨座標發送

<form action="#">
    <input name="test">
    <input type="image" name="imagesubmit" src="https://demo.xiaohuochai.site/submit.jpg" width="99" height="99" alt="測試圖片">
</form>   

submit

type="submit"的input輸入類型用於創建提交表單的按鈕

reset

type="reset"的input輸入類型用於創建重置表單的按鈕

<form action="#" method="get">
    <input>
    <input type="reset" value="Reset" />
    <input type="submit" value="Submit" />
</form>

新增類型

color  定義調色板

tel  定義包含電話號碼的輸入域

email  定義包含email地址的輸入域

url  定義包含URL地址的輸入域

search  定義搜索域

number  定義包含數值的輸入域

range  定義包含一定範圍內數字值的輸入域

date  定義選取日、月、年的輸入域

month  定義選取月、年的輸入域

week  定義選取周、年的輸入域

time  定義選取月、年的輸入域

datetime  定義選取時間、日 月、年的輸入域(UTC時間)

datatime-local  定義選取時間、日 月、年的輸入域(本地時間)

color

type="color"的input輸入類型會創建一個調色板用來選擇顏色,顏色值以URL編碼後的十六進制數值提交。如黑色會以%23000000發送,其中%23是#的URL編碼

注意:safari和IE不支持該類型

【默認樣式】

chrome
    width:44px;
    height:23px;
    border: 1px solid rgb(169,169,169);
    padding: 1px 2px;
firefox
    width:46px;
    height:17px;
    border: 3px solid rgb(169,169,169);
    padding: 6px 0;    
專門建立的學習Q-q-u-n: 731771211,分享學習方法和需要注意的小細節,不停更新最新的教程和學習技巧
(從零基礎開始到前端項目實戰教程,學習工具,全棧開發學習路線以及規劃)

tel

type="tel"的input輸入類型用於表示語義上的電話輸入域,外觀上與type="text"的input輸入類型沒有差異,在手機端會喚出數字鍵盤

<form action="#">
    <input type="tel" placeholder="請輸入11位手機號碼" pattern="\d{11}">    
    <input type="submit">
</form>

email

type="email"的input輸入類型用於表示語義上的e-mail地址輸入域,會自動驗證email域的值,外觀上與type="text"的input輸入類型沒有差異,在手機端會喚出英文鍵盤

email類型的input元素支持multiple屬性

注意:IE9-瀏覽器及safari瀏覽器不支持

<form action="#" >
    <input type="email" name="email" multiple>
    <input type="submit">
</form>

url

type="url"的input輸入類型用於表示語義上的url地址的輸入域,會自動驗證url域的值,外觀上與type="text"的input輸入類型沒有差異

注意:IE9-瀏覽器及safari瀏覽器不支持

<input type="url">

search

type="search"的input輸入類型用於表示語義上的搜索框,外觀上與type="text"的input輸入類型沒有差異

<input type="search">

number

type="number"的input輸入類型用於處理數字輸入,在手機端會喚出數字鍵盤

注意:IE不支持該類型

【默認樣式】

chrome/safari
    border: 2px inset;
    padding-left: 1px;
firefox
    border: 1px inset;
    padding: 2px;

【屬性】

max  規定允許的最大值

min  規定允許的最小值

step  規定合法的數字間隔

value  規定默認值

注意:屬性的取值可爲小數

<input type="number" min="0" max="10" step="0.5" value="6" />

range

type="range"的input輸入類型用於處理包含在一定範圍內的數字輸入,類似於type="number"的input類型

注意:IE9-不支持該類型

【默認樣式】

chrome/safari
    margin: 2px;
firefox
    border: 1px inset;
    padding: 1px;
    margin: 0 9.3px;
IE10+
    padding: 17px 0 32px;

【屬性】

max  規定允許的最大值

min  規定允許的最小值

step  規定合法的數字間隔

value  規定默認值

注意:屬性的取值可爲小數

<input type="range" min="0" max="10" step="0.5" value="6" />

注意:如果不設置min和max屬性,則默認min=0,max=100

HTML5擁有多個可供選取日期和時間的新輸入類型

date

type="date"的input輸入類型用於選取日、月、年

month

type="month"的input輸入類型用於選取月、年

week

type="week"的input輸入類型用於選取周、年

time

type="time"的input輸入類型用於選取時、分

datetime

type="datetime"的input輸入類型用於選取時、日、月、年(UTC時間)

datetime-local

type="datetime-local"的input輸入類型用於選取時、日、月、年(本地時間)

注意:IE和firefox這6種日期類型都不支持,chrome不支持datetime類型

【默認樣式】

chrome/safari
    border: 2px inset;
    padding-left: 1px;
專門建立的學習Q-q-u-n: 731771211,分享學習方法和需要注意的小細節,不停更新最新的教程和學習技巧
(從零基礎開始到前端項目實戰教程,學習工具,全棧開發學習路線以及規劃)
<input type="date"><br><br>
<input type="month"><br><br>
<input type="week"><br><br>
<input type="time"><br><br>
<input type="datetime"><br><br>
<input type="datetime-local">

要預設控件的日期和時間,可以用字符串設置value屬性

【value屬性格式】

date                   YYYY-MM-DD
time                   hh:mm:ss.s
datetime               YYYY-MM-DDThh:mm:ss:sZ
datetime-local           YYYY-MM-DDThh:mm:ss:s
month                 YYYY-MM
week                   YYYY-Wnn

YYYY=年
MM=月
DD=日
hh=小時
mm=分鐘
ss=秒
s=0.1秒
T=日期與時間之間的分隔符
Z=Zulu時間的時區
Wnn=W週數,從1月的第一週開始是1,直到52

該類型的value屬性格式還可以用在min和max的屬性裏,用來創建時間跨度;step可以用來設置移動的刻度

注意:chrome不支持該類型的step設置

<input type="week" value="2015-W36" step="2" min="2015-W25" max="2015-W40">

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章