HTML5详解

本文详细介绍了HTML5的新特性,包括新的语义元素、表单元素、输入类型、图像和媒介元素。同时,讲解了HTML5在浏览器支持、Shiv解决方案、元素迁移等方面的内容。此外,还探讨了HTML5 API,如地理定位、拖放功能、Web存储和应用缓存。通过实例解析,帮助开发者了解如何在实际项目中应用HTML5。
摘要由CSDN通过智能技术生成

typora-root-url: pic

HTML5

一、简介

1、什么是 HTML5?

HTML5 是最新的 HTML 标准。

HTML5 是专门为承载丰富的 web 内容而设计的,并且无需额外插件。

HTML5 拥有新的语义、图形以及多媒体元素。

HTML5 提供的新元素和新的 API 简化了 web 应用程序的搭建。

HTML5 是跨平台的,被设计为在不同类型的硬件(PC、平板、手机、电视机等等)之上运行。

**注释:**在下面的章节中,您将学到如何“帮助”老版本的浏览器处理 HTML5。

2、HTML5 中的新内容

HTML5 的新的文档类型(DOCTYPE)声明非常简单:

<!DOCTYPE html>
The new character encoding (charset) declaration is also very simple:

<meta charset="UTF-8">

实例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>
Content of the document......
</body>

</html>

**注释:**HTML5 中默认的字符编码是 UTF-8。

3、HTML5 - 新的属性语法

HTML5 标准允许 4 中不同的属性语法。

本例演示在 标签中使用的不同语法:

类型 示例
Empty <input type=“text” value=“John Doe” disabled>
Unquoted <input type=“text” value=John Doe>
Double-quoted <input type=“text” value=“John Doe”>
Single-quoted <input type=“text” value=‘John Doe’>

在 HTML5 标准中,根据对属性的需求,可能会用到所有 4 种语法。

4、HTML5 - 新特性

HTML5 的一些最有趣的新特性:

  • 新的语义元素,比如 <header>, <footer>, <article>, and <section>。
  • 新的表单控件,比如数字、日期、时间、日历和滑块。
  • 强大的图像支持(借由 <canvas> 和 <svg>)
  • 强大的多媒体支持(借由 <video> 和 <audio>)
  • 强大的新 API,比如用本地存储取代 cookie。

5、HTML5 - 已被删元素

以下 HTML 4.01 元素已从 HTML5 中删除:

  • <acronym>
  • <applet>
  • <basefont>
  • <big>
  • <center>
  • <dir>
  • <font>
  • <frame>
  • <frameset>
  • <noframes>
  • <strike>
  • <tt>

二、支持

1、浏览器支持

所有现代浏览器都支持 HTML5。

此外,所有浏览器,不论新旧,都会自动把未识别元素当做行内元素来处理。

正因如此,您可以帮助老式浏览器处理”未知的“ HTML 元素。

**注释:**您甚至可以教授石器时代的 IE6 如何处理未知的 HTML 元素。

2、将 HTML5 元素定义为块级元素

HTML5 定义了八个新的语义 HTML 元素。所有都是块级元素。

您可以把 CSS display 属性设置为 block,以确保老式浏览器中正确的行为:

实例:

header, section, footer, aside, nav, main, article, figure {
    display: block; 
}

3、添加新元素

您可以通过浏览器 trick 向 HTML 添加任何新元素:

本例向 HTML 添加了一个名为 的新元素,并为其定义 display 样式:

实例:

<!DOCTYPE html>
<html>

<head>
  <title>Creating an HTML Element</title>
  <script>document.createElement("myHero")</script>
  <style>
  myHero {
     
    display: block;
    background-color: #ddd;
    padding: 50px;
    font-size: 30px;
  } 
  </style> 
</head>

<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

<myHero>My First Hero</myHero>

</body>
</html>

已添加的 JavaScript 语句 document.createElement(“myHero”),仅适用于 IE。

4、Internet Explorer 的问题及解决方案

上述方案可用于所有新的 HTML5 元素,但是:

**注意:**Internet Explorer 8 以及更早的版本,不允许对未知元素添加样式。

幸运的是,Sjoerd Visscher 创造了 “HTML5 Enabling JavaScript”, “the shiv”

<!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

以上代码是一段注释,但是 IE9 的早期版本会读取它(并理解它)。

5、完整的 Shiv 解决方案

实例:

<!DOCTYPE html>
<html>

<head>
  <title>Styling HTML5</title>
  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>

<body>

<h1>My First Article</h1>

<article>
London is the capital city of England. 
It is the most populous city in the United Kingdom, 
with a metropolitan area of over 13 million inhabitants.
</article>

</body>
</html>

引用 shiv 代码的链接必须位于 <head> 元素中,因为 Internet Explorer 需要在读取之前认识所有新元素。

6、HTML5 Skeleton

实例:

<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML5 Skeleton</title>
<meta charset="utf-8">

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->

<style>
body {
     font-family: Verdana, sans-serif; font-size:0.8em;}
header,nav, section,article,footer
{
     border:1px solid grey; margin:5px; padding:8px;}
nav ul {
     margin:0; padding:0;}
nav ul li {
     display:inline; margin:5px;}
</style>
</head>

<body>

<header>
  <h1>HTML5 SKeleton</h1>
</header>

<nav>
<ul>
  <li><a href="html5_semantic_elements.asp">HTML5 Semantic</a></li>
  <li><a href="html5_geolocation.asp">HTML5 Geolocation</a></li>
  <li><a href="html5_canvas.asp">HTML5 Graphics</a></li>
</ul>
</nav>

<section>

<h1>Famous Cities</h1>

<article>
<h2>London</h2>
<p>London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.</p>
</article>

<article>
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</article>

<article>
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</article>

</section>

<footer>
<p>© 2014 W3Schools. All rights reserved.</p>
</footer>

</body>
</html>

三、元素

1、新的语义/结构元素

HTML5 提供的新元素可以构建更好的文档结构:

标签 描述
<article> 定义文档内的文章。
<aside> 定义页面内容之外的内容。
<bdi> 定义与其他文本不同的文本方向。
<details> 定义用户可查看或隐藏的额外细节。
<dialog> 定义对话框或窗口。
<figcaption> 定义 <figure> 元素的标题。
<figure> 定义自包含内容,比如图示、图表、照片、代码清单等等。
<footer> 定义文档或节的页脚。
<header> 定义文档或节的页眉。
<main> 定义文档的主内容。
<mark> 定义重要或强调的内容。
<menuitem> 定义用户能够从弹出菜单调用的命令/菜单项目。
<meter> 定义已知范围(尺度)内的标量测量。
<nav> 定义文档内的导航链接。
<progress> 定义任务进度。
<rp> 定义在不支持 ruby 注释的浏览器中显示什么。
<rt> 定义关于字符的解释/发音(用于东亚字体)。
<ruby> 定义 ruby 注释(用于东亚字体)。
<section> 定义文档中的节。
<summary> 定义 <details> 元素的可见标题。
<time> 定义日期/时间。
<wbr> 定义可能的折行(line-break)。
HTML5 <section> 元素

<section> 元素定义文档中的节。

根据 W3C 的 HTML 文献:“节(section)是有主题的内容组,通常具有标题”。

可以将网站首页划分为简介、内容、联系信息等节。

实例:

<section>
   <h1>WWF</h1>
   <p>The World Wide Fund for Nature (WWF) is....</p>
</section> 
HTML5 <article> 元素

<article> 元素规定独立的自包含内容。

文档有其自身的意义,并且可以独立于网站其他内容进行阅读。

<article> 元素的应用场景:

  • 论坛
  • 博客
  • 新闻

实例:

<article>
   <h1>What Does WWF Do?</h1>
   <p>WWF's mission is to stop the degradation of our planet's natural environment,
  and build a future in which humans live in harmony with nature.</p>
</article> 
嵌套语义元素

在 HTML5 标准中,<article> 元素定义完整的相关元素自包含块。

<section> 元素被定义为相关元素块。

我们能够使用该定义来决定如何嵌套元素吗?不,我们不能!

在因特网上,您会发现 <section> 元素包含 <article> 元素的 HTML 页面,还有 <article> 元素包含 <sections> 元素的页面。

您还会发现 <section> 元素包含 <section> 元素,同时 <article> 元素包含 <article> 元素。

HTML5 <header> 元素

<header> 元素为文档或节规定页眉

一个文档中可以有多个

元素。

下例为一篇文章定义了页眉:

实例:

<article>
   <header>
     <h1>What Does WWF Do?</h1>
     <p>WWF's mission:</p>
   </header>
   <p>WWF's mission is to stop the degradation of our planet's natural environment,
  and build a future in which humans live in harmony with nature.</p>
</article> 
HTML5 <footer> 元素

<footer> 元素为文档或节规定页脚。

<footer> 元素应该提供有关其包含元素的信息。

页脚通常包含文档的作者、版权信息、使用条款链接、联系信息等等。

您可以在一个文档中使用多个 <footer> 元素。

实例:

<footer>
   <p>Posted by: Hege Refsnes</p>
   <p>Contact information: <a href="mailto:someone@example.com">
  someone@example.com</a>.</p>
</footer> 
HTML5 <nav> 元素

<nav> 元素定义导航链接集合。

<nav> 元素旨在定义大型的导航链接块。不过,并非文档中所有链接都应该位于 <nav> 元素中!

实例:

<nav>
<a href="/html/">HTML<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

real_fxyyyyyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值