HTML文档结构
- HTML文档实际上类似一个XML文档,完整的HTML文档包含根元素
<html>
,然后在<html>
中包含<head>
,<body>
等元素,下面是一个典型的HTML文档。
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">
Once upon a time there were three little sisters;and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Llsie</a>and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
</body>
</html>
这个文档在浏览器中显示效果如下:
- HTML文档中的
<...>
的元素称为一个tag元素或者element元素,例如<html>
、<body>
、<title>
、<p>
、<a>
等都是这类的元素,每个tag元素都有对应的结束元素</...>
,例如</html>
、</body>
、</title>
、</p>
、</a>
等。 - 注意HTML中的tag元素的名称是不分大小写的,因此、、是一样的,这一点与XML不同。
- 一个tag元素可以有很多属性,例如
<p class="title">
中的<p>
元素有属性class
,属性值为title
。 - 注意:HTML中除了tag元素之外,穿插于tag元素之间的那些文本也是元素,称为text元素,例如
<title>The Dormouse's story</title>
中的文本The Dormouse's story
也是元素,它是一个text文本元素,它的父节点是<title>
。
HTML文档树
- HTML的结构是一个树状结构,在内存中形成一棵树,例如HTML结构:
<html><head><title>Demo</title></head>
<body>
<div>A<p>B</p>C</div>
<span>D</span>
</body>
</html>
那么对应的文档树为: