HTML 5深入淺出教學(xué)篇之一
介紹
HTML 5: 根元素, 元數(shù)據(jù)元素, 腳本元素
根元素 - doctype, html
元數(shù)據(jù)元素 - head, title, base, link, meta, style
腳本元素 - script, noscript
示例
1、doctype - 文檔類型element/root/doctype.html
- <!--
- <!doctype html> - 聲明文檔類型為 HTML5 文檔
- -->
- <!doctype html>
- <html>
- <head>
- <title>doctype</title>
- </head>
- <body>
- </body>
- </html>
2、html - 文檔的根元素element/root/html.html
- <!doctype html>
- <!--
- html - 文檔的根元素,如需指定語(yǔ)言,則設(shè)置“lang”屬性即可
- -->
- <html lang="zh-CN">
- <head>
- <title>html</title>
- </head>
- <body>
- </body>
- </html>
#p#
3、head - 頭容器element/metadata/head.html
- <!doctype html>
- <html>
- <!--
- head - 頭容器,可包含如下標(biāo)簽: title, base, link, meta, style, script。其中必須要有 title 標(biāo)簽
- -->
- <head>
- <title>head</title>
- <base />
- <link />
- <meta />
- <style></style>
- <script type="text/javascript"></script>
- </head>
- <body>
- </body>
- </html>
4、title - 文檔標(biāo)題element/metadata/title.html
- <!doctype html>
- <html>
- <head>
- <!--
- title - 文檔標(biāo)題
- -->
- <title>我是標(biāo)題</title>
- </head>
- <body>
- <script type="text/javascript">
- var title = document.getElementsByTagName("title")[0];
- alert(title.text);
- </script>
- </body>
- </html>
5、base - 設(shè)置文檔的默認(rèn)地址和鏈接的默認(rèn)打開(kāi)方式element/metadata/base.html
- <!doctype html>
- <html>
- <head>
- <title>base</title>
- <!--
- base - 設(shè)置一些默認(rèn)值
- href - 指定當(dāng)前文檔的默認(rèn)地址,不指定的話默認(rèn)地址為當(dāng)前文檔的 url。文檔中相對(duì)路徑的解析后的絕對(duì)地址為:默認(rèn)地址 + 相對(duì)路徑
- target - 指定文檔中鏈接的默認(rèn)打開(kāi)方式。_blank, _parent, _self, _top
- -->
- <base href="http://pic.cnblogs.com/avatar/" target="_blank" />
- </head>
- <body>
- <!--
- a 標(biāo)簽的鏈接打開(kāi)方式為 _blank
- img 標(biāo)簽的圖片絕對(duì)地址為 http://pic.cnblogs.com/avatar/a14540.jpg
- -->
- <a href="http://webabcd.cnblogs.com/">
- <img src="a14540.jpg" alt="baby" />
- </a>
- </body>
- </html>
6、link - 定義兩個(gè)文檔之間的關(guān)系,一般用于引入樣式表element/metadata/link.html
- <!doctype html>
- <html>
- <head>
- <title>link</title>
- <!--
- link - 定義兩個(gè)文檔之間的關(guān)系,一般用于引入樣式表,示例如下
- rel - 指定文檔間的關(guān)系,對(duì)于樣式表來(lái)說(shuō)此屬性的值為 stylesheet。link 標(biāo)簽必須要有 rel 屬性
- type - link 所接入的文檔的類型
- href - link 所接入的文檔的地址
- title - link 所接入的文檔的標(biāo)題,對(duì)于樣式表來(lái)說(shuō),可以在 meta 里指定默認(rèn)樣式表的 title,從而只使用指定 title 的樣式表
- disabled, relList(只讀), media, hreflang, sizes(只讀)
- -->
- <link rel="stylesheet" type="text/css" href="http://www.www.www/css.css" />
- </head>
- <body>
- </body>
- </html>
7、meta - 文檔相關(guān)的元數(shù)據(jù)element/metadata/meta.html
- <!doctype html>
- <html>
- <head>
- <title>meta</title>
- <link rel="stylesheet" type="text/css" href="http://www.cnblogs.com/assets/css1.css" title="css1" />
- <link rel="stylesheet" type="text/css" href="http://www.cnblogs.com/assets/css2.css" title="css2" />
- <!--
- meta - 文檔相關(guān)的元數(shù)據(jù)??捎脤傩匀缦拢簄ame, http-equiv, content, charset
- -->
- <!--
- name, content 組合的示例如下(不全)
- -->
- <!--定義關(guān)鍵字-->
- <meta name="keywords" content="html5, flash, silverlight" />
- <!--定義文檔的描述信息-->
- <meta name="description" content="介紹 html5 中的 meta 標(biāo)簽" />
- <!--定義文檔的作者-->
- <meta name="author" content="webabcd" />
- <!--定義文檔的生成工具-->
- <meta name="generator" content="EditPlus" />
- <!--如果把此 html5 文檔看成一個(gè)程序,則此處用于定義程序的名稱-->
- <meta name="application-name" content="meta 標(biāo)簽的介紹" />
- <!--
- http-equiv, content 組合的示例如下(不全)
- -->
- <!--定義文檔內(nèi)容的語(yǔ)言-->
- <meta http-equiv="content-language" content="zh-CN" />
- <!--定義文檔內(nèi)容的類型-->
- <meta http-equiv="content-type" content="text/html" />
- <!--定義文檔所使用的樣式表的 title,從而在有多個(gè)樣式表的時(shí)候,只使用指定 title 的樣式表。本例中會(huì)強(qiáng)制只使用 title 為 css1 的樣式表-->
- <meta http-equiv="default-style" content="css1" />
- <!--文檔每 100 秒刷新一次-->
- <meta http-equiv="refresh" content="100" />
- <!--設(shè)置 cookie-->
- <meta http-equiv="set-cookie" content="author=webabcd;" />
- <!--定義文檔的編碼類型-->
- <meta charset="utf-8">
- </head>
- <body>
- webabcd
- <script type="text/javascript">
- // 獲取 meta 中設(shè)置的 cookie
- alert(document.cookie);
- </script>
- </body>
- </html>
css1.css
- body
- {
- color: Red;
- }
css2.css
- body
- {
- color: Green;
- font-size: 24px;
- }
8、style - 定義文檔的樣式信息element/metadata/style.html
- <!doctype html>
- <html>
- <head>
- <title>style</title>
- <!--
- style - 定義文檔的樣式信息??捎脤傩匀缦? disabled, media, type, scoped
- scoped - bool 類型的屬性,如果為 true,則代表樣式只能應(yīng)用到 style 元素的父元素及其子元素(對(duì)于 scoped 為 false 的 style 只能寫(xiě)在 head 內(nèi))
- -->
- <style>
- div { font-size: 24px; }
- </style>
- </head>
- <body>
- <div>
- 我和我的子都應(yīng)該是紅色的
- <style scoped>
- div { color:Red; }
- </style>
- </div>
- <div>
- 如果我是紅色的,也就是說(shuō)瀏覽器不支持 style 的 scoped(注:目前所有瀏覽器都不支持 style 的 scoped)
- </div>
- </body>
- </html>
#p#
9、script - 用于定義客戶端腳本element/script/script.html
- <!doctype html>
- <html>
- <head>
- <title>script</title>
- <!--
- script - 用于定義客戶端腳本,可用屬性如下:src, async, defer, type, charset
- type - 腳本的 MIME 類型,此屬性必須要有
- src - 外部腳本的 url 地址,如果指定了 src,那么 script 標(biāo)簽必須是空的
- charset - 腳本的編碼類型
- defer - bool 類型。如果為 true 的話,則腳本在頁(yè)面解析完后執(zhí)行,即在 DOMContentLoaded 事件之前執(zhí)行,會(huì)按照 script 在頁(yè)面的出現(xiàn)順序執(zhí)行,不阻塞頁(yè)面解析(界面解析的過(guò)程中,并行下載腳本)
- async - bool 類型。如果為 true 的話,則在頁(yè)面解析的過(guò)程中會(huì)異步下載腳本,腳本下載完馬上執(zhí)行(肯定會(huì)在 window 的 onload 事件之前執(zhí)行),不阻塞頁(yè)面解析(界面解析的過(guò)程中,并行下載腳本)
- -->
- <!--
- 引用兩段腳本,無(wú) async 時(shí)或 defer 時(shí),必然先執(zhí)行完 script1 后再執(zhí)行 script2
- 如果被標(biāo)記為 async,假設(shè) script2 先下載完,script1 后下載完的話,那么會(huì)先執(zhí)行 script2, 再執(zhí)行 script1
- -->
- <script type="text/javascript" src="http://www.cnblogs.com/assets/script1.js" async></script>
- <script type="text/javascript" src="http://www.cnblogs.com/assets/script2.js" async></script>
- </head>
- <body>
- </body>
- </html>
script1.js
- alert("script1 completed");
- /*偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js*/
- /*偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js*/
- /*偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js 偽造一個(gè)大容量的 js*/
- ...
script2.js
- alert("script2 completed");
10、noscript - 定義當(dāng)瀏覽器不支持腳本的時(shí)候所顯示的內(nèi)容element/script/noscript.html
- <!doctype html>
- <html>
- <head>
- <title>noscript</title>
- </head>
- <body>
- <script type="text/javascript">
- alert("您的瀏覽器支持腳本");
- </script>
- <!--
- noscript - 定義當(dāng)瀏覽器不支持腳本的時(shí)候所顯示的內(nèi)容
- -->
- <noscript>
- 您的瀏覽器不支持腳本
- </noscript>
- </body>
- </html>
編者按:“此篇文章可能相比以前HTML來(lái)說(shuō),并沒(méi)有太大的差距,總體來(lái)說(shuō),以上內(nèi)容相對(duì)基礎(chǔ),請(qǐng)繼續(xù)關(guān)注后續(xù)文章”
原文鏈接:http://www.cnblogs.com/webabcd/archive/2011/09/15/2176955.html