开发者必须知道的HTML5十五大新特性

HTML5想必大家都很熟悉了,因为太多的媒体在讨论这一技术。然而,你能准确地说出HTML5带来了哪些新特性吗?本文总结了HTML5带来的15项你必须知道的新特性。
一起来看下:

1.新的文档类型  (New Doctype)

目前许多网页还在使用XHTML 1.0 并且要在第一行像这样声明文档类型:

双击代码全选

1

2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

在HTML5中,上面那种声明方式将失效。下面是HTML5中的声明方式:

双击代码全选

1

<!DOCTYPE html>

 

2.脚本和链接无需type  (No More Types for Scripts and Links)

在HTML4或XHTML中,你需要用下面的几行代码来给你的网页添加CSS和JavaScript文件。

双击代码全选

1

2

3

<pre class="brush:html;toolbar:false;"><link rel="stylesheet" href="path/to/stylesheet.css" type="text/css" />

<script type="text/javascript" src="path/to/script.js"></script></pre><p>

</p>

 

而在HTML5中,你不再需要指定类型属性。因此,代码可以简化如下:

双击代码全选

1

2

3

<pre class="brush:html;toolbar:false;"><link rel="stylesheet" href="path/to/stylesheet.css" />

<script src="path/to/script.js"></script></pre><p>

</p>

 

3.语义Header和Footer (The Semantic Header and Footer)

在HTML4或XHTML中,你需要用下面的代码来声明"Header"和"Footer"。

双击代码全选

1

2

3

4

5

6

7

<div id="header">

...

</div>

..........

<div id="footer">

...

</div>

 

在HTML5中,有两个可以替代上述声明的元素,这可以使代码更简洁。

双击代码全选

1

2

3

4

5

6

7

<pre class="brush:html;toolbar:false;"><header>

...

</header>

<footer>

...

</footer></pre><p>

</p>

 

4.Hgroup

在 HTML5中,有许多新引入的元素,hgroup就是其中之一。假设我的网站名下面紧跟着一个子标题,我可以用<h1> 和<h2>标签来分别定义。然而,这种定义没有说明这两者之间的关系。而且,h2标签的使用会带来更多问题,比如该页面上还有其他标题的时 候。
在HTML5中,我们可以用hgroup元素来将它们分组,这样就不会影响文件的大纲。

双击代码全选

1

2

3

4

5

6

<header>

<hgroup>

  <h1> Recall Fan Page </h1>

  <h2> Only for people who want the memory of a lifetime. </h2>

</hgroup>

</header>

 

5.标记元素 (Mark Element)

你可以把它当做高亮标签。被这个标签修饰的字符串应当和用户当前的行动相关。比如说,当我在某博客中搜索“Open your Mind”时,我可以利用一些JavaScript将出现的词组用<mark>修饰一下。

双击代码全选

1

2

3

<pre class="brush:html;toolbar:false;"><h3> Search Results </h3>

<p> They were interrupted, just after Quato said, <mark>"Open your Mind"</mark>. </p></pre><p>

</p>

 

6.图形元素 (Figure Element)

在HTML4或XHTML中,下面的这些代码被用来修饰图片的注释。

双击代码全选

1

2

3

<pre class="brush:html;toolbar:false;"><img src="path/to/image" alt="About image" />

<p>Image of Mars. </p></pre><p>

</p>

 

然而,上述代码没有将文字和图片内在联系起来。因此,HTML5引入了<figure>元素。当和<figcaption>结合起来后,我们可以语义化地将注释和相应的图片联系起来。

双击代码全选

1

2

3

4

5

6

7

<pre class="brush:html;toolbar:false;"><figure>

<img src="path/to/image" alt="About image" />

<figcaption>

  <p>This is an image of something interesting. </p>

</figcaption>

</figure></pre><p>

</p>

 

7.重新定义<small> (Small Element redefined)

在 HTML4或XHTML中,<small>元素已经存在。然而,却没有如何正确使用这一元素的完整说明。在HTML5 中,<small>被用来定义小字。试想下你网站底部的版权状态,根据对此元素新的HTML5定义,<small>可以正确地诠 释这些信息。

8.占位符 (Placeholder)

在HTML4或XHTML中,你需要用JavaScript来给文本框添加占位符。比如,你可以提前设置好一些信息,当用户开始输入时,文本框中的文字就消失。
而在HTML5中,新的“placeholder”就简化了这个问题。

9.必要属性 (Required Attribute)

HTML5中的新属性“required”指定了某一输入是否必需。有两种方法声明这一属性。

双击代码全选

1

2

<input type="text" name="someInput" required>

<input type="text" name="someInput" required="required">

 

当文本框被指定必需时,如果空白的话表格就不能提交。下面是一个如何使用的例子。

双击代码全选

1

2

3

4

5

6

<pre class="brush:html;toolbar:false;"><form method="post" action="">

<label for="someInput"> Your Name: </label>

<input type="text" id="someInput" name="someInput" placeholder="Douglas Quaid" required>

<button type="submit">Go</button>

</form></pre><p>

</p>

 

在上面那个例子中,如果输入内容空且表格被提交,输入框将被高亮显示。

10.Autofocus 属性 (Autofocus Attribute)

同样,HTML5的解决方案消除了对JavaScript的需要。如果一个特定的输入应该是“选择”或聚焦,默认情况下,我们现在可以利用自动聚焦属性。

双击代码全选

1

2

3

4

5

6

7

8

9

10

11

12

13

<pre class="brush:html;toolbar:false;"><input type="text" name="someInput" placeholder="Douglas Quaid" required autofocus></pre><p>

</p>

  <div style=" display:block; width:100%; padding-top:15px; margin:0px auto; height:90px; overflow:hidden; text-align:center;"><script src="/2011/ads/tech_content_end_468x60.js"></script><script type="text/javascript"><!--

google_ad_client = "pub-5977682010997732";

/* 468x60,  09-9-9 */

google_ad_slot = "4048873275";

google_ad_width = 468;

google_ad_height = 60;

//-->

</script>

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

</script><ins style="display:inline-table;border:none;height:60px;margin:0;padding:0;position:relative;visibility:visible;width:468px"><ins id="aswift_1_anchor" style="display:block;border:none;height:60px;margin:0;padding:0;position:relative;visibility:visible;width:468px"><iframe allowtransparency="true" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&&s.handlers,h=H&&H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&&d&&(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&&s.log.push(i)}" vspace="0" id="aswift_1" name="aswift_1" style="left:0;position:absolute;top:0;" scrolling="no" width="468" frameborder="0" height="60"></iframe></ins></ins>

</div>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Grow your business fast with

Suku