aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/learn/html/introduction_to_html/getting_started/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/learn/html/introduction_to_html/getting_started/index.html')
-rw-r--r--files/zh-cn/learn/html/introduction_to_html/getting_started/index.html683
1 files changed, 683 insertions, 0 deletions
diff --git a/files/zh-cn/learn/html/introduction_to_html/getting_started/index.html b/files/zh-cn/learn/html/introduction_to_html/getting_started/index.html
new file mode 100644
index 0000000000..34fa761a4e
--- /dev/null
+++ b/files/zh-cn/learn/html/introduction_to_html/getting_started/index.html
@@ -0,0 +1,683 @@
+---
+title: 开始学习 HTML
+slug: learn/HTML/Introduction_to_HTML/Getting_started
+tags:
+ - HTML
+ - 元素
+ - 属性
+ - 指南
+translation_of: Learn/HTML/Introduction_to_HTML/Getting_started
+---
+<div>{{LearnSidebar}}</div>
+
+<div>{{NextMenu("Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML", "Learn/HTML/Introduction_to_HTML")}}</div>
+
+<p class="summary">本文将从 HTML 最基础的部分讲起,对元素(Element)、属性(Attribute)以及可能涉及的一些重要术语进行介绍,并明确它们在语言中所处的位置。本文还会讲解 HTML 元素和页面的组织方式,以及其他一些重要的基本语言特性。学习的过程中,我们会使用 HTML 做一些好玩的事情。</p>
+
+<table class="learn-box standard-table">
+ <tbody>
+ <tr>
+ <th scope="row">预备知识:</th>
+ <td>具备计算机基础知识,安装了<a href="/zh-CN/Learn/Getting_started_with_the_web/Installing_basic_software">基础软件环境</a>,了解基本的<a href="/zh-CN/Learn/Getting_started_with_the_web/Dealing_with_files">文件组织方法</a>。</td>
+ </tr>
+ <tr>
+ <th scope="row">学习目标:</th>
+ <td>熟悉 HTML 语言的基础知识,掌握几个 HTML 元素的基本用法。</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="什么是_HTML">什么是 HTML?</h2>
+
+<p>{{glossary("HTML")}} (HyperText Markup Language) 不是一门编程语言,而是一种用来告知浏览器如何组织页面的<strong>标记语言</strong>。HTML 可复杂、可简单,一切取决于开发者。它由一系列的<strong>元素({{Glossary("Element", "elements")}})</strong>组成,这些元素可以用来包围不同部分的内容,使其以某种方式呈现或者工作。 一对标签( {{Glossary("Tag", "tags")}})可以为一段文字或者一张图片添加超链接,将文字设置为斜体,改变字号,等等。 例如下面一行内容:</p>
+
+<pre>我的猫咪脾气爆:)</pre>
+
+<p>可以将这行文字封装成一个段落(Paragraph){{htmlelement("p")}}元素来使其在单独一行呈现:</p>
+
+<pre class="brush: html">&lt;p&gt;我的猫咪脾气爆:)&lt;/p&gt;</pre>
+
+<div class="note">
+<p><strong>注:</strong>HTML 标签不区分大小写。也就是说,输入标签时既可以使用大写字母也可以使用小写字母。例如,标签 {{htmlelement("title")}} 写作<code>&lt;title&gt;<font face="x-locale-heading-primary, zillaslab, Palatino, Palatino Linotype, x-locale-heading-secondary, serif"><span style="background-color: #fff3d4;">、</span></font></code><code>&lt;TITLE&gt;</code>、<code>&lt;Title&gt;</code>、<code>&lt;TiTlE&gt;</code>,等等都可以正常工作。不过,从一致性、可读性等各方面来说,最好仅使用小写字母。</p>
+</div>
+
+<h2 id="剖析一个_HTML_元素">剖析一个 HTML 元素</h2>
+
+<p>让我们进一步探讨我们的段落元素:</p>
+
+<p><img alt="" src="https://mdn.mozillademos.org/files/16475/element.png" style="display: block; height: 255px; margin: 0px auto; width: 821px;"></p>
+
+<p>这个元素的主要部分有:</p>
+
+<ol>
+ <li><strong>开始标签</strong>(Opening tag):包含元素的名称(本例为 p),被左、右角括号所包围。表示元素从这里开始或者开始起作用 —— 在本例中即段落由此开始。</li>
+ <li><strong>结束标签</strong>(Closing tag):与开始标签相似,只是其在元素名之前包含了一个斜杠。这表示着元素的结尾 —— 在本例中即段落在此结束。初学者常常会犯忘记包含结束标签的错误,这可能会产生一些奇怪的结果。</li>
+ <li><strong>内容</strong>(Content):元素的内容,本例中就是所输入的文本本身。</li>
+ <li><strong>元素</strong>(Element):开始标签、结束标签与内容相结合,便是一个完整的元素。</li>
+</ol>
+
+<h3 id="主动学习:创建第一个_HTML_元素">主动学习:创建第一个 HTML 元素</h3>
+
+<p>通过使用标签<code>&lt;em&gt;</code>和<code>&lt;/em&gt;</code>(在前面放置<code>&lt;em&gt;</code>打开元素,在后面放置<code>&lt;/em&gt;</code>关闭元素)——这使得行内容变成斜体强调!你可以在“输出”区域中实时查看更改更新。</p>
+
+<p>如果写错了,可随时按【重置】按钮重新开始,如果实在想不出来,可按【显示答案】按钮查看答案。</p>
+
+<div class="hidden">
+<h6 id="Playable_code">Playable code</h6>
+
+<pre class="brush: html">&lt;!DOCTYPE html&gt;
+&lt;html lang="zh-CN"&gt;
+ &lt;head&gt;
+ &lt;meta charset="utf-8"&gt;
+ &lt;style&gt;
+ body { font-family: '微软雅黑', Helvetica, Arial, sans-serif; margin: 10px; background: #f5f9fa; }
+ h2 { font-size: 16px; }
+ code, textarea { font-family: Consolas, Menlo, monospace; }
+ .output { min-height: 50px; }
+ .input { min-height: 100px; width: 95%; }
+ .a11y-label { margin: 0; text-align: right; font-size: 0.7rem; width: 98%; }
+ .controls { width: 96%; text-align: right; }
+ &lt;/style&gt;
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;h2&gt;实时输出&lt;/h2&gt;
+ &lt;div class="output"&gt;&lt;/div&gt;
+
+ &lt;h2&gt;可编辑代码&lt;/h2&gt;
+ &lt;p class="a11y-label"&gt;按 ESC 退出编辑区域,按 Tab 可插入制表符 &lt;code&gt;'\t'&lt;/code&gt; &lt;/p&gt;
+ &lt;textarea id="code" class="input"&gt;&lt;/textarea&gt;
+
+ &lt;div class="controls"&gt;
+ &lt;button id="btn-reset"&gt;重置&lt;/button&gt;
+ &lt;button id="btn-solution"&gt;显示答案&lt;/button&gt;
+ &lt;/div&gt;
+ &lt;script&gt;
+ const btnReset = document.getElementById('btn-reset');
+ const btnSolution = document.getElementById('btn-solution');
+ const blockOutput = document.querySelector('.output');
+ const blockInput = document.querySelector('.input');
+ const original = '刀枪剑戟 斧钺钩叉';
+ const answer = '&lt;em&gt;刀枪剑戟 斧钺钩叉&lt;/em&gt;';
+ let userEntry = "";
+
+ init();
+ btnReset.addEventListener('click', init);
+
+ btnSolution.addEventListener('click', () =&gt; {
+ if (btnSolution.textContent === '显示答案') {
+ blockInput.value =
+ blockOutput.innerHTML = answer;
+ btnSolution.textContent = '隐藏答案';
+ } else {
+ blockInput.value =
+ blockOutput.innerHTML = userEntry;
+ btnSolution.textContent = '显示答案';
+ }
+ });
+
+ blockInput.addEventListener('keydown', (e) =&gt; {
+ switch (e.key) {
+ case 'Tab':
+ e.preventDefault();
+ insertAtCursor('\t');
+ break;
+ case "Escape":
+ blockInput.blur();
+ break;
+ }
+ });
+
+ blockInput.addEventListener('keyup', () =&gt; {
+ userEntry = blockInput.value;
+ blockOutput.innerHTML = blockInput.value;
+ if (btnSolution.textContent === '隐藏答案') {
+ btnSolution.textContent = '显示答案';
+ }
+ });
+
+ function init() {
+ userEntry =
+ blockOutput.innerHTML =
+ blockInput.value = original;
+ btnSolution.textContent = '显示答案';
+ }
+
+ function insertAtCursor(text) {
+ const scrollPos = blockInput.scrollTop;
+ const cursorPos = blockInput.selectionStart;
+
+ const front = blockInput.value.substring(0, cursorPos);
+ const back = blockInput.value.substring(
+ blockInput.selectionEnd, blockInput.value.length);
+
+ blockInput.value = front + text + back;
+ blockInput.selectionStart =
+ blockInput.selectionEnd = cursorPos + text.length;
+ blockInput.focus();
+ blockInput.scrollTop = scrollPos;
+ }
+ &lt;/script&gt;
+ &lt;/body&gt;
+&lt;/html&gt;</pre>
+</div>
+
+<p>{{ EmbedLiveSample('Playable_code', 700, 400, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<h3 id="嵌套元素">嵌套元素</h3>
+
+<p>你也可以把元素放到其它元素之中——这被称作嵌套。如果我们想要表明我们的小猫脾气很暴躁,可以将<strong>“爆”</strong>嵌套在{{htmlelement("strong")}} 中,意味着这个单词被着重强调:</p>
+
+<pre class="brush: html">&lt;p&gt;我的猫咪脾气&lt;strong&gt;爆&lt;/strong&gt;:)&lt;/p&gt;</pre>
+
+<p>你需要确保元素被正确的嵌套:在上面的例子中我们先打开{{htmlelement("p")}}元素,然后才打开{{htmlelement("strong")}}元素,因此必须先将{{htmlelement("strong")}}元素关闭,然后再去关闭{{htmlelement("p")}}元素。下面的例子是错误的:</p>
+
+<pre class="example-bad brush: html">&lt;p&gt;我的猫咪脾气&lt;strong&gt;爆:)&lt;/p&gt;&lt;/strong&gt;</pre>
+
+<p>所有的元素都需要正确的打开和关闭,这样才能按你所想的方式展现。如果像上述的例子一样进行了错误的嵌套,那么浏览器会去猜测你想要表达的意思,但很有可能会得出错误的结果。所以永远不要这么做!</p>
+
+<h3 id="块级元素和内联元素">块级元素和内联元素</h3>
+
+<p>在HTML中有两种你需要知道的重要元素类别,块级元素和内联元素。</p>
+
+<ul>
+ <li>块级元素在页面中以块的形式展现 —— 相对于其前面的内容它会出现在新的一行,其后的内容也会被挤到下一行展现。块级元素通常用于展示页面上结构化的内容,例如段落、列表、导航菜单、页脚等等。一个以block形式展现的块级元素不会被嵌套进内联元素中,但可以嵌套在其它块级元素中。</li>
+ <li>内联元素通常出现在块级元素中并环绕文档内容的一小部分,而不是一整个段落或者一组内容。内联元素不会导致文本换行:它通常出现在一堆文字之间例如超链接元素{{htmlelement("a")}}或者强调元素{{htmlelement("em")}}和 {{htmlelement("strong")}}。</li>
+</ul>
+
+<p>看一看下面的例子:</p>
+
+<pre class="brush: html">&lt;em&gt;第一&lt;/em&gt;&lt;em&gt;第二&lt;/em&gt;&lt;em&gt;第三&lt;/em&gt;
+
+&lt;p&gt;第四&lt;/p&gt;&lt;p&gt;第五&lt;/p&gt;&lt;p&gt;第六&lt;/p&gt;
+</pre>
+
+<p>{{htmlelement("em")}} 是一个内联元素,所以就像你在下方可以看到的,第一行代码中的三个元素都没有间隙的展示在了同一行。而{{htmlelement("p")}}是一个块级元素,所以第二行代码中的每个元素分别都另起了新的一行展现,并且每个段落间都有一些间隔(这是因为默认的浏览器有着默认的展示{{htmlelement("p")}}元素的<a href="/en-US/docs/Learn/CSS/Introduction_to_CSS">CSS styling</a>)。</p>
+
+<p>{{ EmbedLiveSample('块级元素和内联元素', 700, 200, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<div class="note">
+<p><strong>注</strong>: HTML5重新定义了元素的类别:见 <a href="https://html.spec.whatwg.org/multipage/indices.html#element-content-categories">元素内容分类</a>(<a href="/zh-CN/docs/Web/Guide/HTML/Content_categories">译文</a>)。尽管这些新的定义更精确,但却比上述的 “块级元素” 和 “内联元素” 更难理解,因此在之后的讨论中仍使用旧的定义。</p>
+</div>
+
+<div class="note">
+<p><strong>注</strong>: 在这篇文章中提到的“块”和“内联”,不应该与 <a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Box_model#Types_of_CSS_boxes">the types of CSS boxes</a> 中的同名术语相混淆. 尽管他们默认是相关的,但改变CSS显示类型并不会改变元素的分类,也不会影响它可以包含和被包含于哪些元素。防止这种混淆也是HTML5摒弃这些术语的原因之一。</p>
+</div>
+
+<div class="note">
+<p><strong>注</strong>: 你可以查阅包含了块级元素和内联元素列表的参考页面—see <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements">Block-level elements</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements">Inline elements</a>.</p>
+</div>
+
+<h3 id="空元素">空元素</h3>
+
+<p>不是所有元素都拥有开始标签,内容,结束标签。一些元素只有一个标签,通常用来在此元素所在位置插入/嵌入一些东西。例如:元素{{htmlelement("img")}}是用来在元素{{htmlelement("img")}}所在位置插入一张指定的图片。例子如下:</p>
+
+<pre class="brush: html">&lt;img src="https://roy-tian.github.io/learning-area/extras/getting-started-web/beginner-html-site/images/firefox-icon.png"&gt;</pre>
+
+<p>显示如下:</p>
+
+<p>{{ EmbedLiveSample('空元素', 700, 300, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<div class="note">
+<p><strong>注</strong>: 空元素(Empty elements) 有时也被叫作 <em>void elements</em>.</p>
+</div>
+
+<h2 id="属性">属性</h2>
+
+<p>元素也可以拥有属性,如下:</p>
+
+<p><img alt='&amp;amp;amp;lt;p class="editor-note">我的猫咪脾气爆&amp;amp;amp;lt;/p>' src="https://mdn.mozillademos.org/files/16476/attribute.png" style="display: block; margin: 0px auto;"></p>
+
+<p>属性包含元素的额外信息,这些信息不会出现在实际的内容中。在上述例子中,这个class属性给元素赋了一个识别的名字(id),这个名字此后可以被用来识别此元素的样式信息和其他信息。</p>
+
+<p>一个属性必须包含如下内容:</p>
+
+<ol>
+ <li>一个空格,在属性和元素名称之间。(如果已经有一个或多个属性,就与前一个属性之间有一个空格。)</li>
+ <li>属性名称,后面跟着一个等于号。</li>
+ <li>一个属性值,由一对引号“ ”引起来。</li>
+</ol>
+
+<h3 id="学习实践:为一个元素添加属性">学习实践:为一个元素添加属性</h3>
+
+<p>另一个例子是关于元素{{htmlelement("a")}}的——元素{{htmlelement("a")}}是锚,它使被标签包裹的内容成为一个超链接。此元素也可以添加大量的属性,其中几个如下:</p>
+
+<ul>
+ <li><code>href</code>: 这个属性声明超链接的web地址,当这个链接被点击浏览器会跳转至href声明的web地址。例如:<code>href="https://www.mozilla.org/"</code>。</li>
+ <li><code>title</code>: 标题<code>title</code>属性为超链接声明额外的信息,比如你将链接至的那个页面。例如:<code>title="The Mozilla homepage"</code>。当鼠标悬停在超链接上面时,这部分信息将以工具提示的形式显示。</li>
+ <li><code>target</code>: 目标<code>target</code>属性用于指定链接如何呈现出来。例如,<code>target="_blank"</code>将在新标签页中显示链接。如果你希望在当前标签页显示链接,忽略这个属性即可。</li>
+</ul>
+
+<p>编辑下面的文本框中的内容,使之变成指向任一个你喜欢的web地址的链接。首先,添加&lt;a&gt;元素,然后为它添加href属性和title属性。你可以即时的在输出区域看到你修改的内容。你应该可以看到一个连接,当鼠标移上此链接时会显示title属性值,当点击此链接时会跳转到href指定的web地址。记住:在元素名和属性名之间以及两个属性之间要有一个空格。</p>
+
+<p>如果写错了,可随时按【重置】按钮重新开始,如果实在想不出来,可按【显示答案】按钮查看答案。</p>
+
+<div class="hidden">
+<h6 id="Playable_code2">Playable code2</h6>
+
+<pre class="brush: html">&lt;!DOCTYPE html&gt;
+&lt;html lang="zh-CN"&gt;
+ &lt;head&gt;
+ &lt;meta charset="utf-8"&gt;
+ &lt;style&gt;
+ body { font-family: '微软雅黑', Helvetica, Arial, sans-serif; margin: 10px; background: #f5f9fa; }
+ h2 { font-size: 16px; }
+ code, textarea { font-family: Consolas, Menlo, monospace; }
+ .output { min-height: 50px; }
+ .input { min-height: 100px; width: 95%; }
+ .a11y-label { margin: 0; text-align: right; font-size: 0.7rem; width: 98%; }
+ .controls { width: 96%; text-align: right; }
+ &lt;/style&gt;
+
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;h2&gt;实时输出&lt;/h2&gt;
+ &lt;div class="output"&gt;&lt;/div&gt;
+
+ &lt;h2&gt;可编辑代码&lt;/h2&gt;
+ &lt;p class="a11y-label"&gt;按 ESC 退出编辑区域,按 Tab 可插入制表符 &lt;code&gt;'\t'&lt;/code&gt; &lt;/p&gt;
+ &lt;textarea id="code" class="input"&gt;&lt;/textarea&gt;
+
+ &lt;div class="controls"&gt;
+ &lt;button id="btn-reset"&gt;重置&lt;/button&gt;
+ &lt;button id="btn-solution"&gt;显示答案&lt;/button&gt;
+ &lt;/div&gt;
+ &lt;script&gt;
+ const btnReset = document.getElementById('btn-reset');
+ const btnSolution = document.getElementById('btn-solution');
+ const blockOutput = document.querySelector('.output');
+ const blockInput = document.querySelector('.input');
+ const original = '&lt;p&gt;欲练葵花宝典,需引刀自宫&lt;/p&gt;';
+ const answer = '&lt;p&gt;欲练&lt;a href="https://zh.wikipedia.org/zh-hans/葵花宝典" title="葵花宝典简介" target="_blank"&gt;葵花宝典&lt;/a&gt;,需引刀自宫&lt;/p&gt;';
+ let userEntry = "";
+
+ init();
+ btnReset.addEventListener('click', init);
+
+ btnSolution.addEventListener('click', () =&gt; {
+ if (btnSolution.textContent === '显示答案') {
+ blockInput.value =
+ blockOutput.innerHTML = answer;
+ btnSolution.textContent = '隐藏答案';
+ } else {
+ blockInput.value =
+ blockOutput.innerHTML = userEntry;
+ btnSolution.textContent = '显示答案';
+ }
+ });
+
+ blockInput.addEventListener('keydown', (e) =&gt; {
+ switch (e.key) {
+ case 'Tab':
+ e.preventDefault();
+ insertAtCursor('\t');
+ break;
+ case "Escape":
+ blockInput.blur();
+ break;
+ }
+ });
+
+ blockInput.addEventListener('keyup', () =&gt; {
+ userEntry = blockInput.value;
+ blockOutput.innerHTML = blockInput.value;
+ if (btnSolution.textContent === '隐藏答案') {
+ btnSolution.textContent = '显示答案';
+ }
+ });
+
+ function init() {
+ userEntry =
+ blockOutput.innerHTML =
+ blockInput.value = original;
+ btnSolution.textContent = '显示答案';
+ }
+
+ function insertAtCursor(text) {
+ const scrollPos = blockInput.scrollTop;
+ const cursorPos = blockInput.selectionStart;
+
+ const front = blockInput.value.substring(0, cursorPos);
+ const back = blockInput.value.substring(
+ blockInput.selectionEnd, blockInput.value.length);
+
+ blockInput.value = front + text + back;
+ blockInput.selectionStart =
+ blockInput.selectionEnd = cursorPos + text.length;
+ blockInput.focus();
+ blockInput.scrollTop = scrollPos;
+ }
+ &lt;/script&gt;
+ &lt;/body&gt;
+&lt;/html&gt;
+</pre>
+</div>
+
+<p>{{ EmbedLiveSample('Playable_code2', 700, 350, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<div class="blockIndicator note">
+<p><strong>译注:</strong>可到 Github 在线使用这个“<a class="external external-icon" href="https://roy-tian.github.io/learning-area/extras/tools/playable-code">代码操场</a>”。</p>
+</div>
+
+<h3 id="布尔属性">布尔属性</h3>
+
+<p>有时你会看到没有值的属性,它是合法的。这些属性被称为布尔属性,他们只能有跟它的属性名一样的属性值。例如{{htmlattrxref("disabled", "input")}} 属性,他们可以标记表单输入使之变为不可用(变灰色),此时用户不能向他们输入任何数据。</p>
+
+<pre>&lt;input type="text" disabled="disabled"&gt;</pre>
+
+<p>方便起见,我们完全可以将其写成以下形式(我们还提供了一个非禁止输入的表单元素供您参考,以作为对比):</p>
+
+<pre class="brush: html">&lt;!-- 使用disabled属性来防止终端用户输入文本到输入框中 --&gt;
+&lt;input type="text" disabled&gt;
+
+&lt;!-- 下面这个输入框没有disabled属性,所以用户可以向其中输入 --&gt;
+&lt;input type="text"&gt;
+</pre>
+
+<p>上面两段HTML代码产生的效果如下:</p>
+
+<p>{{ EmbedLiveSample('布尔属性', 700, 100, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<h3 id="省略包围属性值的引号">省略包围属性值的引号</h3>
+
+<p>当你浏览那些粗糙的web网站,你将会看见各种各样奇怪的标记风格,其中就有不给属性值添加引号。在某些情况下它是被允许的,但是其他情况下会破坏你的标记。例如,我们可以写一个只拥有一个href属性的链接,如下:</p>
+
+<pre class="example-bad brush: html">&lt;a href=<code>https://www.mozilla.org/</code>&gt;收藏页面&lt;/a&gt;</pre>
+
+<p>然而,当我们再添加一个title属性时就会出错,如下:</p>
+
+<pre class="example-bad brush: html">&lt;a href=<code>https://www.mozilla.org/</code> title=The Mozilla homepage&gt;收藏页面&lt;/a&gt;</pre>
+
+<p>此时浏览器会误解你的标记,它会把title属性理解为三个属性——title的属性值为"The“,另外还有两个布尔属性“<code>Mozilla</code>”和“<code>homepage</code>”。看下面的例子,它明显不是我们所期望的,并且在这个编码里面它会报错或者出现异常行为。试一试把鼠标移动到链接上,看会显示什么title属性值!</p>
+
+<p>{{ EmbedLiveSample('省略包围属性值的引号', 700, 100, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<p>我们建议始终添加引号——这样可以避免很多问题,并且使代码更易读。</p>
+
+<h3 id="单引号或者双引号?">单引号或者双引号?</h3>
+
+<p>在目前为止,本章内容所有的属性都是由双引号来包裹。也许在一些HTML中,你以前也见过单引号。这只是风格的问题,你可以从中选择一个你喜欢的。以下两种情况都可以:</p>
+
+<pre class="brush: html">&lt;a href="http://www.example.com"&gt;示例站点链接&lt;/a&gt;
+
+&lt;a href='http://www.example.com'&gt;示例站点链接&lt;/a&gt;</pre>
+
+<p>但你应该注意单引号和双引号不能在一个属性值里面混用。下面的语法是错误的:</p>
+
+<pre class="example-bad brush: html">&lt;a href="http://www.example.com'&gt;示例站点链接&lt;/a&gt;</pre>
+
+<p>在一个HTML中已使用一种引号,你可以在此引号中嵌套另外一种引号:</p>
+
+<pre class="brush: html">&lt;a href="http://www.example.com" title="你觉得'好玩吗'?"&gt;示例站点链接&lt;/a&gt;</pre>
+
+<p>如果你想将引号当作文本显示在html中,你就必须使用<a href="#实体引用:_在HTML中包含特殊字符">实体引用</a>。</p>
+
+<h2 id="剖析HTML文档">剖析HTML文档</h2>
+
+<p>学习了一些HTML元素的基础知识,这些元素单独一个是没有意义的。现在我们来学习这些特定元素是怎么被结合起来,从而形成一个完整的HTML页面的:</p>
+
+<pre class="brush: html">&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+ &lt;head&gt;
+ &lt;meta charset="utf-8"&gt;
+ &lt;title&gt;我的测试站点&lt;/title&gt;
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;p&gt;这是我的页面&lt;/p&gt;
+ &lt;/body&gt;
+&lt;/html&gt;</pre>
+
+<p>分析如下:</p>
+
+<ol>
+ <li><code>&lt;!DOCTYPE html&gt;</code>: 声明文档类型. 很久以前,早期的HTML(大约1991年2月),文档类型声明类似于链接,规定了HTML页面必须遵从的良好规则,能自动检测错误和其他有用的东西。使用如下:
+
+ <pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;</pre>
+ 然而这种写法已经过时了,这些内容已成为历史。只需要知道 <code>&lt;!DOCTYPE html&gt;</code> 是最短有效的文档声明。</li>
+ <li><code>&lt;html&gt;&lt;/html&gt;</code>: <code>&lt;html&gt;</code>元素。这个元素包裹了整个完整的页面,是一个根元素。</li>
+ <li><code>&lt;head&gt;&lt;/head&gt;</code>: <code>&lt;head&gt;元素</code>. 这个元素是一个容器,它包含了所有你想包含在HTML页面中但不想在HTML页面中显示的内容。这些内容包括你想在搜索结果中出现的关键字和页面描述,CSS样式,字符集声明等等。以后的章节能学到更多关于&lt;head&gt;元素的内容。</li>
+ <li><code>&lt;meta charset="utf-8"&gt;</code>: 这个元素设置文档使用utf-8字符集编码,utf-8字符集包含了人类大部分的文字。基本上他能识别你放上去的所有文本内容。毫无疑问要使用它,并且它能在以后避免很多其他问题。</li>
+ <li><code>&lt;title&gt;&lt;/title&gt;</code>: 设置页面标题,出现在浏览器标签上,当你标记/收藏页面时它可用来描述页面。</li>
+ <li><code>&lt;body&gt;&lt;/body&gt;</code>: <code>&lt;body&gt;</code>元素。 包含了你访问页面时所有显示在页面上的内容,文本,图片,音频,游戏等等。</li>
+</ol>
+
+<h3 id="学习实践:为HTML文档添加一些特征">学习实践:为HTML文档添加一些特征</h3>
+
+<p>如果你想在你的本地练习写一些HTML页面,你可以这样做:</p>
+
+<ol>
+ <li>复制上面的HTML页面例子。</li>
+ <li>在编辑器创建一个新文件。</li>
+ <li>粘贴代码到这个文件。</li>
+ <li>保存为<code>index.html</code>.</li>
+</ol>
+
+<div class="note">
+<p><strong>注:</strong>可在 <a href="https://github.com/roy-tian/learning-area/blob/master/html/introduction-to-html/getting-started/index.html">学习区代码仓库</a> 上查看该示例。</p>
+</div>
+
+<p>你可以打开浏览器看看这段代码的效果是什么样的,然后改变代码刷新浏览器看看新的结果。最开始的代码是这样的效果:</p>
+
+<p><img alt="A simple HTML page that says This is my page" src="https://mdn.mozillademos.org/files/12279/template-screenshot.png" style="display: block; height: 365px; margin: 0px auto; width: 595px;">所以在这段练习中, 你可以用你的电脑在本地编写运行代码,如上所述, 你也可以在下面的简单可编辑窗口编辑它 (此时这个简单的可编辑窗口仅显示&lt;body&gt;标签内的内容.) 我们希望你能够实践以下步骤:</p>
+
+<ul>
+ <li>就在{{htmlelement("body")}} 元素开始标签下方,为这个文档添加一个主标题。这个主标题应该被包含在<code>&lt;h1&gt;</code>开始标签和<code>&lt;/h1&gt;</code>结束标签之间。</li>
+ <li>编辑这个段落以包含一些你感兴趣的文本。</li>
+ <li>把字词包含在开始标记<code>&lt;strong&gt;</code>和结束标记<code>&lt;/strong&gt;</code>之间可以使他们以粗体显示,从而突出任何重要的字词。</li>
+ <li>在你的文档中添加一个超文本链接,<a href="https://developer.mozilla.org/zh-CN/docs/Learn/HTML/Introduction_to_HTML/Getting_started#%E5%AD%A6%E4%B9%A0%E5%AE%9E%E8%B7%B5%EF%BC%9A%E4%B8%BA%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0%E6%B7%BB%E5%8A%A0%E5%B1%9E%E6%80%A7">如前所述</a>。</li>
+ <li>在段落下方向你的文档添加一张图片,<a href="https://developer.mozilla.org/zh-CN/docs/learn/HTML/Introduction_to_HTML/Getting_started#%E7%A9%BA%E5%85%83%E7%B4%A0">如前所述</a>。如果你尝试对不同的图片(在你的本地电脑或是在Web的其他位置上)添加链接,那你就更棒了。</li>
+</ul>
+
+<p>如果写错了,可随时按【重置】按钮重新开始,如果实在想不出来,可按【显示答案】按钮查看答案。</p>
+
+<div class="hidden">
+<h6 id="Playable_code3">Playable code3</h6>
+
+<pre class="brush: html">&lt;!DOCTYPE html&gt;
+&lt;html lang="zh-CN"&gt;
+ &lt;head&gt;
+ &lt;meta charset="utf-8"&gt;
+ &lt;style&gt;
+ body { font-family: '微软雅黑', Helvetica, Arial, sans-serif; margin: 10px; background: #f5f9fa; }
+ h2 { font-size: 16px; }
+ code, textarea { font-family: Consolas, Menlo, monospace; }
+ .output { min-height: 50px; }
+ .input { min-height: 100px; width: 95%; }
+ .a11y-label { margin: 0; text-align: right; font-size: 0.7rem; width: 98%; }
+ .controls { width: 96%; text-align: right; }
+ &lt;/style&gt;
+
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;h2&gt;实时输出&lt;/h2&gt;
+ &lt;div class="output"&gt;&lt;/div&gt;
+
+ &lt;h2&gt;可编辑代码&lt;/h2&gt;
+ &lt;p class="a11y-label"&gt;按 ESC 退出编辑区域,按 Tab 可插入制表符 &lt;code&gt;'\t'&lt;/code&gt; &lt;/p&gt;
+ &lt;textarea id="code" class="input"&gt;&lt;/textarea&gt;
+
+ &lt;div class="controls"&gt;
+ &lt;button id="btn-reset"&gt;重置&lt;/button&gt;
+ &lt;button id="btn-solution"&gt;显示答案&lt;/button&gt;
+ &lt;/div&gt;
+ &lt;script&gt;
+ const btnReset = document.getElementById('btn-reset');
+ const btnSolution = document.getElementById('btn-solution');
+ const blockOutput = document.querySelector('.output');
+ const blockInput = document.querySelector('.input');
+ const original = '&lt;p&gt;相思无用,惟别而已。别期若有定,千般煎熬又何如?莫道黯然销魂,何处柳暗花明?&lt;/p&gt;';
+ const answer =
+`&lt;h1&gt;经典回忆&lt;/h1&gt;
+&lt;p&gt;
+ 相思无用,惟别而已。别期若有定,千般煎熬又何如?莫道黯然销魂,何处&lt;strong&gt;柳暗花明&lt;/strong&gt;?&lt;br&gt;
+ ——《&lt;a href="https://zh.wikipedia.org/zh-hans/神鵰俠侶"&gt;神雕侠侣&lt;/a&gt;》
+&lt;/p&gt;
+&lt;img src="https://roy-tian.github.io/learning-area/extras/tools/playable-code/images/sdxl.jfif"&gt;`;
+ let userEntry = "";
+
+ init();
+ btnReset.addEventListener('click', init);
+
+ btnSolution.addEventListener('click', () =&gt; {
+ if (btnSolution.textContent === '显示答案') {
+ blockInput.value =
+ blockOutput.innerHTML = answer;
+ btnSolution.textContent = '隐藏答案';
+ } else {
+ blockInput.value =
+ blockOutput.innerHTML = userEntry;
+ btnSolution.textContent = '显示答案';
+ }
+ });
+
+ blockInput.addEventListener('keydown', (e) =&gt; {
+ switch (e.key) {
+ case 'Tab':
+ e.preventDefault();
+ insertAtCursor('\t');
+ break;
+ case "Escape":
+ blockInput.blur();
+ break;
+ }
+ });
+
+ blockInput.addEventListener('keyup', () =&gt; {
+ userEntry = blockInput.value;
+ blockOutput.innerHTML = blockInput.value;
+ if (btnSolution.textContent === '隐藏答案') {
+ btnSolution.textContent = '显示答案';
+ }
+ });
+
+ function init() {
+ userEntry =
+ blockOutput.innerHTML =
+ blockInput.value = original;
+ btnSolution.textContent = '显示答案';
+ }
+
+ function insertAtCursor(text) {
+ const scrollPos = blockInput.scrollTop;
+ const cursorPos = blockInput.selectionStart;
+
+ const front = blockInput.value.substring(0, cursorPos);
+ const back = blockInput.value.substring(
+ blockInput.selectionEnd, blockInput.value.length);
+
+ blockInput.value = front + text + back;
+ blockInput.selectionStart =
+ blockInput.selectionEnd = cursorPos + text.length;
+ blockInput.focus();
+ blockInput.scrollTop = scrollPos;
+ }
+ &lt;/script&gt;
+ &lt;/body&gt;
+&lt;/html&gt;
+
+</pre>
+</div>
+
+<p>{{ EmbedLiveSample('Playable_code3', 700, 600,"", "", "hide-codepen-jsfiddle")}}</p>
+
+<h3 id="HTML中的空白">HTML中的空白</h3>
+
+<p>在上面的例子中,你可能已经注意到了在代码中包含了很多的空格——这是没有必要的;下面的两个代码片段是等价的:</p>
+
+<pre class="brush: html">&lt;p&gt;狗 狗 很 呆 萌。&lt;/p&gt;
+
+&lt;p&gt;狗 狗 很
+ 呆 萌。&lt;/p&gt;</pre>
+
+<p>无论你在HTML元素的内容中使用多少空格(包括空白字符,包括换行),当渲染这些代码的时候,HTML解释器会将连续出现的空白字符减少为一个单独的空格符。</p>
+
+<p>那么为什么我们会在HTML元素的嵌套中使用那么多的空白呢? 答案就是为了可读性 —— 如果你的代码被很好地进行格式化,那么就很容易理解你的代码是怎么回事,反之就只有聚做一团的混乱.。在我们的HTML代码中,我们让每一个嵌套的元素以两个空格缩进。 你使用什么风格来格式化你的代码取决于你 (比如所对于每层缩进使用多少个空格),但是你应该坚持使用某种风格。</p>
+
+<h2 id="实体引用:_在HTML中包含特殊字符">实体引用: 在HTML中包含特殊字符</h2>
+
+<p>在HTML中,字符 <code>&lt;</code>, <code>&gt;</code>,<code>"</code>,<code>'</code> 和 <code>&amp;</code> 是特殊字符. 它们是HTML语法自身的一部分, 那么你如何将这些字符包含进你的文本中呢, 比如说如果你真的想要在文本中使用符号&amp;或者小于号, 而不想让它们被浏览器视为代码并被解释?</p>
+
+<p>我们必须使用字符引用 —— 表示字符的特殊编码, 它们可以在那些情况下使用. 每个字符引用以符号&amp;开始, 以分号(;)结束.</p>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">原义字符</th>
+ <th scope="col">等价字符引用</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>&lt;</td>
+ <td>&amp;lt;</td>
+ </tr>
+ <tr>
+ <td>&gt;</td>
+ <td>&amp;gt;</td>
+ </tr>
+ <tr>
+ <td>"</td>
+ <td>&amp;quot;</td>
+ </tr>
+ <tr>
+ <td>'</td>
+ <td>&amp;apos;</td>
+ </tr>
+ <tr>
+ <td>&amp;</td>
+ <td>&amp;amp;</td>
+ </tr>
+ </tbody>
+</table>
+
+<p>在下面的例子中你可以看到两个段落,它们在谈论web技术:</p>
+
+<pre class="brush: html">&lt;p&gt;HTML 中用 &lt;p&gt; 来定义段落元素。&lt;/p&gt;
+
+&lt;p&gt;HTML 中用 &amp;lt;p&amp;gt; 来定义段落元素&lt;/p&gt;</pre>
+
+<p>在下面的实时输出中,你会看到第一段是错误的,因为浏览器会认为第二个&lt;p&gt;是开始一个新的段落! 第二段是正确的,因为我们用字符引用来代替了角括号('&lt;'和'&gt;'符号).</p>
+
+<p>{{ EmbedLiveSample('实体引用:_在HTML中包含特殊字符', 700, 200, "", "", "hide-codepen-jsfiddle") }}</p>
+
+<div class="note">
+<p><strong>提示</strong>: 维基百科上有一个包含所有可用HTML字符实体引用的列表:<a href="http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references">XML和HTML字符实体引用列表</a>。</p>
+</div>
+
+<h2 id="HTML注释">HTML注释</h2>
+
+<p>如同大部分的编程语言一样,在HTML中有一种可用的机制来在代码中书写注释 —— 注释是被浏览器忽略的,而且是对用户不可见的,它们的目的是允许你描述你的代码是如何工作的和不同部分的代码做了什么等等。 如果你在半年后重新返回你的代码库,而且不能记起你所做的事情 —— 或者当你处理别人的代码的时候, 那么注释是很有用的.</p>
+
+<p>为了将一段HTML中的内容置为注释,你需要将其用特殊的记号&lt;!--和--&gt;包括起来, 比如:</p>
+
+<pre class="brush: html">&lt;p&gt;我在注释外!&lt;/p&gt;
+
+&lt;!-- &lt;p&gt;我在注释内!&lt;/p&gt; --&gt;</pre>
+
+<p>正如你下面所见的那样,第一段出现在了实时输出中,但是第二段却没有。</p>
+
+<p>{{ EmbedLiveSample('HTML注释', 700, 100,"", "", "hide-codepen-jsfiddle") }}</p>
+
+<h2 id="总结">总结</h2>
+
+<p>你已经来到了这篇文章的结尾 —— 希望你享受你的基础的HTML学习的旅程。 在这里你应该可以理解HTML语言的全貌, 它在基础的级别是如何工作,而且可以使用一些元素和属性。 在这个模块的后续文章中,我们会深入一些你已经见过的东西的细节,并且介绍一些新的HTML的特性。未完待续!</p>
+
+<div class="note">
+<p><strong>提示</strong>: 现在,你将开始学习更多关于HTML的知识,你可能也想了解一些层叠样式列表(<a href="https://developer.mozilla.org/zh-CN/docs/Learn/CSS">CSS</a>)的基础知识。CSS是一种用来设计网页样式的语言(比如,用它改变字体、颜色或页面布局等)。你很快就会发现,HTML和CSS能很好地协调配合。</p>
+</div>
+
+<div>{{NextMenu("Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML", "Learn/HTML/Introduction_to_HTML")}}</div>
+
+<h2 id="本章目录">本章目录</h2>
+
+<ul>
+ <li><a href="/zh-CN/docs/learn/HTML/Introduction_to_HTML/Getting_started">开始学习 HTML</a></li>
+ <li><a href="/zh-CN/docs/learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML">“头”里有什么?HTML 元信息</a></li>
+ <li><a href="/zh-CN/docs/learn/HTML/Introduction_to_HTML/HTML_text_fundamentals">HTML 文字处理初步</a></li>
+ <li><a href="/zh-CN/docs/Learn/HTML/Introduction_to_HTML/Creating_hyperlinks">创建超链接</a></li>
+ <li><a href="/zh-CN/docs/Learn/HTML/Introduction_to_HTML/Advanced_text_formatting">高级文字格式</a></li>
+ <li><a href="/zh-CN/docs/learn/HTML/Introduction_to_HTML/文件和网站结构">文档和站点结构</a></li>
+ <li><a href="/zh-CN/docs/Learn/HTML/Introduction_to_HTML/Debugging_HTML">HTML 调试</a></li>
+ <li><a href="/zh-CN/docs/Learn/HTML/Introduction_to_HTML/Marking_up_a_letter">课程测验:为信件排版</a></li>
+ <li><a href="/zh-CN/docs/Learn/HTML/Introduction_to_HTML/Structuring_a_page_of_content">课程测验:构建内容丰富的网页</a></li>
+</ul>