input元素的23種type類型

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

傳統(tǒng)類型

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

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

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

radio??定義單選按鈕

checkbox??定義復(fù)選框

hidden??定義隱藏的輸入字段

button??定義可點擊按鈕(多數(shù)情況下,用于通過JavaScript啟動腳本)

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

reset??定義重置按鈕。重置按鈕會清除表單中的所有數(shù)據(jù)

submit??定義提交按鈕。提交按鈕會把表單數(shù)據(jù)發(fā)送到服務(wù)器

text

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

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;

【重置樣式】

padding: 0;
border: 1px solid;

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

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

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

<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"定義單選按鈕,允許用戶從給定數(shù)目的選擇中選一個選項。同一組按鈕,name值一定要一致

注意:radio類型的input元素無法設(shè)置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"定義多選按鈕,允許用戶在給定數(shù)目的選擇中選擇一個或多個選項。同一組的按鈕,name取值一定要一致

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

【默認樣式】

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"定義隱藏輸入類型用于在表單中增加對用戶不可見,但需要提交的額外數(shù)據(jù)

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

//點擊提交按鈕后,隱藏域的內(nèi)容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輸入類型定義圖像形式的提交按鈕,該類型可以設(shè)置width、height、src、alt這四個屬性

用圖片作為提交按鈕會一起發(fā)送點擊在圖片上的x和y坐標,這樣可以與服務(wù)器端圖片地圖結(jié)合使用,如果圖片有name屬性,也會隨坐標發(fā)送

<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輸入類型用于創(chuàng)建提交表單的按鈕

reset

type="reset"的input輸入類型用于創(chuàng)建重置表單的按鈕

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

新增類型

color??定義調(diào)色板

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

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

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

search??定義搜索域

number??定義包含數(shù)值的輸入域

range??定義包含一定范圍內(nèi)數(shù)字值的輸入域

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

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

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

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

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

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

color

type="color"的input輸入類型會創(chuàng)建一個調(diào)色板用來選擇顏色,顏色值以URL編碼后的十六進制數(shù)值提交。如黑色會以%23000000發(fā)送,其中%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;    

tel

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

<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輸入類型用于處理數(shù)字輸入,在手機端會喚出數(shù)字鍵盤

注意:IE不支持該類型

【默認樣式】

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

【屬性】

max??規(guī)定允許的最大值

min??規(guī)定允許的最小值

step??規(guī)定合法的數(shù)字間隔

value??規(guī)定默認值

注意:屬性的取值可為小數(shù)

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

range

type="range"的input輸入類型用于處理包含在一定范圍內(nèi)的數(shù)字輸入,類似于type="number"的input類型

注意:IE9-不支持該類型

【默認樣式】

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

【屬性】

max??規(guī)定允許的最大值

min??規(guī)定允許的最小值

step??規(guī)定合法的數(shù)字間隔

value??規(guī)定默認值

注意:屬性的取值可為小數(shù)

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

注意:如果不設(shè)置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;

<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">

要預(yù)設(shè)控件的日期和時間,可以用字符串設(shè)置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時間的時區(qū)
Wnn=W周數(shù),從1月的第一周開始是1,直到52

該類型的value屬性格式還可以用在min和max的屬性里,用來創(chuàng)建時間跨度;step可以用來設(shè)置移動的刻度

注意:chrome不支持該類型的step設(shè)置

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

一個不是計算機專業(yè)的理科生,轉(zhuǎn)行學前端

如果你也有一個編程夢,這是咱們的前端學習QQ群:784-783-012

(在線學習,8個月時間,目前就業(yè),廣州工作,月薪16k)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容