官术网_书友最值得收藏!

HTML5 input types

HTML5 comes with a whole host of new input types. These new types are designed to provide formatting, validation, and in some cases, selectors. For touch devices some of them provide a different set of keys for the keyboard. Not all of the new input types are supported by all browsers yet. Fortunately for us, if a browser doesn't support a type it will just display it as a normal text field. Unfortunately for us, you can't depend on the browser to provide the correct formatted data if the unsupported types are only shown as text fields. So make sure you have a backup plan if you are going to use them.

Here are a few of the more useful new input types with images of the ones that are supported by Chrome.

Note

See examples in Chapter 3/input-types/input-types.html.

Color

The color input type is used to choose a color. When clicked it usually displays a color picker. The value is a hex color specifier (for example, #FF0000). This control isn't widely supported for now, so use with caution.

<input type="color" value="#FF0000"/>

Date

The date input type is used to select a date. When clicked it usually displays a date picker. The value is a date string in the format yyyy-mm-dd (for example, 2013-01-23). You may also specify the min and max attributes in the same date format to limit the date span:

<input type="date" value="2013-01-23" min="2013-01-01"/>

Email

The email input type is used to enter an e-mail address. It looks and behaves like a standard text field. On touch devices the keyboard usually changes to provide e-mail symbols such as the @ sign and .com:

<input type="email" value="foo@bar.com"/>

Number

The number input type is used to enter a number. It is usually displayed with up and down buttons (a spinner control) that change the value by the step amount when clicked. On touch devices the keyboard may change to a number pad. There are a few attributes you can use to restrict the field:

  • min: This specifies the minimum value allowed
  • max: This specifies the maximum value allowed
  • step: This specifies the amount by which value changes when you click on the up or down spinner buttons
    <input type="number" value="23" min="0" max="100" step="1"/>

Range

The range input type is used to choose a value from a range of values. This is nearly identical to the number input type, except that it is usually displayed as a slider control. It has the same attributes as the number input type.

<input type="range" value="20" min="0" max="100" step="10"/>

Time

The time input type is used to select a time. When clicked it may display a time picker or you can set the time using the spinners. The value is a 24-hour format time string in the format hh:mm:ss (for example, 13:35:15).

<input type="time" value="13:35:15"/>

URL

The url input type is used to enter a URL. Like the email type, touch devices usually display a keyboard optimized for entering a URL.

<input type="url" value="http://www.worldtreesoftware.com"/>

Datalist

In addition to these new input types, a new <datalist> form element has been added in HTML5. It is used to add a drop-down list of hints to a text field. When the user starts typing in the text field, all of the list options that match the letters being typed will be shown in a dropdown under the field. The user can select one of the options to automatically fill in the field.

You associate a <datalist> element with a text field by setting an ID on the <datalist> element, and referencing it with the list attribute of an <input> element.

<input type="text" list="color-list"/>
<datalist id="color-list">
    <option value="Red"/>
    <option value="Orange"/>
    <option value="Yellow"/>
    <option value="Green"/>
    <option value="Blue"/>
    <option value="Purple"/>
</datalist>
Note

Because implementation of the new input types is spotty at this times, be cautious when using them. Using a number field isn't going to cause many problems if it's not supported; the user can still enter a number in the text field. But something like the color field, if not supported, shows as a text field. Will your user be willing to enter a color in hex code in that case?

Autofocus

There's one more useful addition to HTML5 input elements. A new autofocus attribute was added to set the focus on a particular <input> element when the page is first loaded. Previously we did this in our application by calling the jQuery focus() method for the <input id="new-task-name"> element. We can do the same thing in HTML5 by just adding the autofocus attribute.

<input type="text" autofocus id="new-task-name".../>
主站蜘蛛池模板: 苍南县| 齐齐哈尔市| 鹤山市| 榆林市| 崇州市| 汾阳市| 景东| 蓬莱市| 吕梁市| 濮阳市| 嫩江县| 上思县| 景德镇市| 涪陵区| 布尔津县| 洱源县| 香港 | 达拉特旗| 汤阴县| 进贤县| 白沙| 新巴尔虎左旗| 乃东县| 方正县| 大田县| 永城市| 安仁县| 黄梅县| 获嘉县| 灌南县| 常宁市| 黎川县| 兴业县| 长沙市| 徐州市| 娱乐| 宁河县| 刚察县| 尼玛县| 青川县| 定远县|