Home > Web Front-end > H5 Tutorial > Concise version of HTML5 study notes (6): New attributes (1)

Concise version of HTML5 study notes (6): New attributes (1)

黄舟
Release: 2017-01-21 16:46:29
Original
1911 people have browsed it

Media attributes under a and area

In order to maintain consistency with the link element, the a element and area element also add the media attribute, which is only valid when href exists. The media attribute means what media/device the target URL is optimized for. The default value is all. For detailed syntax specifications, please visit: http://dev.w3.org/csswg/css3-mediaqueries/#media0

Code example:

<a href="att_a_media.asp?output=print" media="print and (resolution:300dpi)">
HTML5 a media attribute.
</a>
Copy after login

hreflang, type, rel attributes under area

In order to maintain consistency with the a element and link element, the area element adds hreflang, type, rel and other attributes.

Attribute

Value

Description

hreflang

language_code

Specifies the language of the target URL

media

media query

Specifies which media/device the target URL is optimized for

rel

alternate, author, bookmark, external, help , license, next, nofollow, noreferrer, prefetch, prev, search, sidebar, tag

Specifies the relationship between the current document and the target URL

type

mime_type

Specifies the MIME type of the target URL

The target attribute under base

The target attribute under base is the same as the target attribute of a. Many older versions of browsers have long ago Supported.

Note 1: target must be declared before all connection elements.

Note 2: If there are multiple declarations, the first one shall prevail.

<!DOCTYPE html>
<html>
    <head>
        <title>This is an example for the <base> element</title>
        <base href="http://www.example.com/news/index.html">
    </head>
    <body>
        <p>Visit the <a href="archives.html">archives</a>.</p>
    </body>
</html>
Copy after login

Click on the link above to jump to http://www.example.com/news/archives.html.

The charset attribute under meta

charset is used to define the encoding method of the document. If this attribute is defined in XML, the value of charset must be case-insensitive ASCII so that match UTF-8, because XML documents are forced to use UTF-8 as the encoding method.

Note: The charset attribute on the meta attribute has no effect in XML documents. It is only for the convenience of direct migration with XHTML.

Cannot declare multiple meta elements with the charset attribute.

In HTML4, we have to define it like this:

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
Copy after login

In HTML5, we just define it like this:

<meta charset="ISO-8859-1">
Copy after login

autofocus Attribute

HTML5 adds an autofocus attribute to input, select, textarea and button elements (hidden input cannot be used), which provides a declarative way to define when the page loads From now on, focus automatically acts on the current element. Using autofocus can improve the user experience. For example, if we set it on the login page, the focus will be automatically set to the textbox of the user name after the page is loaded.

<input maxlength="256" name="loginName" value="" autofocus>
<input type="submit" value="Login">
Copy after login

Note 1: The autofocus attribute is declared once per page.

Note 2: It is not necessary to set autofocus on a page.

placeholder属性

input和textarea元素新增加了placeholder属性,该属性是提升用户输入内容。当用户点击的时候,该内容文本自动消失,离开焦点并且值为空的话,再次显示。以前我们都是使用JavaScript代码来实现,其实蛮复杂的,有了placeholder属性就爽了,直接写成下面下这样的代码:

<input type="username" placeholder="请输入你的用户名">
Copy after login

form属性

form属性(不是

元素),是一个划时代的属性,它允许你将表单里的表单控件声明在表单外门,只需要在相应的控件上设置form属性为指定的表单的id就行了,不需要非得把元素声明在元素里了,解放啦。

代码如下:

<label>Email:
 <input type="email" form="foo" name="email">
</label>
<form id="foo"></form>
Copy after login
支持该属性的元素有:input, output, select, textarea, button, label, object和fieldset。

required属性

required属性是一个验证属性,表明该控件是必填项,在submit表单之前必须填写。可用的元素是:input, select和textarea(例外: type类型为hidden, image或类似submit的input元素)。

如果在select上使用required属性,那就得设置一个带有空值的占位符option。代码如下:

<label>Color: <select name=color required>
 <option value="">Choose one
 <option>Red
 <option>Green
 <option>Blue
</select></label>
Copy after login

fieldset下的disabled属性

当fieldset的设置disabled属性时,其所有的子控件都被禁用掉了,但不包括legend里的元素。name属性是用来脚本访问的。

代码1:

<form>
<fieldset name="clubfields" disabled>
 <legend> <label>
  <input type=checkbox name=club onchange="form.clubfields.disabled = !checked">
  Use Club Card
 </label> </legend>
 <p><label>Name on card: <input name=clubname required></label></p>
 <p><label>Card number: <input name=clubnum required pattern="[-0-9]+"></label></p>
 <p><label>Expiry date: <input name=clubexp type=month></label></p>
</fieldset>
</form>
Copy after login

当点击legend里的checkbox的时候,会自动切换fieldset子元素的disabled状态。

代码2:

<form>
<fieldset name="clubfields">
    <legend>
        <label>
            <input type="checkbox" name="club" onchange="form.clubfields.disabled = !checked">
            Use Club Card
        </label>
    </legend>
    <p>
        <label>
            Name on card:
            <input name="clubname" required></label></p>
    <fieldset name="numfields">
        <legend>
            <label>
                <input type="radio" checked name="clubtype" onchange="form.numfields.disabled = !checked">
                My card has numbers on it
            </label>
        </legend>
        <p>
            <label>
                Card number:
                <input name="clubnum" required pattern="[-0-9]+"></label></p>
    </fieldset>
    <fieldset name="letfields" disabled>
        <legend>
            <label>
                <input type="radio" name="clubtype" onchange="form.letfields.disabled = !checked">
                My card has letters on it
            </label>
        </legend>
        <p>
            <label>
                Card code:
                <input name="clublet" required pattern="[A-Za-z]+"></label></p>
    </fieldset>
</fieldset>
</form>
Copy after login

在这个例子,当你外面的 "Use Club Card" checkbox没有选中的时候,里面的子控件都是被禁用的,如果选中了,两个radiobutton都可用了,然后可以选择哪一个子fieldset你想让它可用。

input下的新属性(autocomplete, min, max, multiple, pattern, step)

input下增加了几个用于约束输入内容的属性(autocomplete, min, max, multiple, pattern和step),目前只有部分浏览器支持required和autocomplete属性,其它属性尚未支持。

autocomplete 属性规定输入字段是否应该启用自动完成功能, 自动完成允许浏览器预测对字段的输入。当用户在字段开始键入时,浏览器基于之前键入过的值,应该显示出在字段中填写的选项。

<form action="demo_form.asp" method="get" autocomplete="on">
  First name:<input type="text" name="fname" /><br />
  Last name: <input type="text" name="lname" /><br />
  E-mail: <input type="email" name="email" autocomplete="off" /><br />
  <input type="submit" />
</form>
Copy after login

注释:autocomplete 属性适用于 ,以及下面的 类型:text, search, url, telephone, email, password, datepickers, range 以及 color。

另外也可以声明一个list属性,用来和存放数据的datalist元素关联:

<form>
<label>Homepage: <input name=hp type=url list=hpurls></label>
<datalist id=hpurls>
 <option value="http://www.google.com/" label="Google">
 <option value="http://www.reddit.com/" label="Reddit">
</datalist>
</form>
Copy after login

当input为空的时候,双击它,就会弹出提示选项(选项内容就是定义的label(Google/Reddit))。选择一个label就会将对应的value地址更新到input里(目前FF支持)。

datalist的声明形式可以有多种:

<datalist id="breeds">
   <option value="Abyssinian">
   <option value="Alpaca">
   <!-- ... -->
</datalist>
或者
<datalist id="breeds">
  <label>
   or select one from the list:
   <select name="breed">
    <option value=""> (none selected)
    <option>Abyssinian
    <option>Alpaca
    <!-- ... -->
   </select>
  </label>
 </datalist>
Copy after login

另外,当input的type为image的时候,input还支持width和height属性用来指定图片的大小。

The above is the concise version of HTML5 study notes (6): the content of new attributes (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template