1. 程式人生 > 實用技巧 >XHTML 元素必須正確巢狀

XHTML 元素必須正確巢狀

XHTML 元素 - 語法規則

  • XHTML 元素必須正確巢狀
  • XHTML 元素必須始終關閉
  • XHTML 元素必須小寫
  • XHTML 文件必須有一個根元素

XHTML 元素必須正確巢狀

在 HTML 中,某些元素可以不正確地彼此巢狀在一起,就像這樣:

<b><i>This text is bold and italic</b></i>

  

在 XHTML 中,所有元素必須正確地彼此巢狀,就像這樣:

<b><i>This text is bold and italic</i></b>

  

XHTML 元素必須始終關閉

這是錯誤的:



<p>This is a paragraph
<p>This is another paragraph

  

這是正確的:

<p>This is a paragraph</p>
<p>This is another paragraph</p>

  

空元素也必須關閉

這是錯誤的:

A break: <br>
A horizontal rule: <hr>
An image: <img src="happy.gif" alt="Happy face">

  

這是正確的:

A break: <br />
A horizontal rule: <hr />
An image: <img src="happy.gif" alt="Happy face" />

  

XHTML 元素必須小寫

這是錯誤的:

<BODY>
<P>This is a paragraph</P>
</BODY>

  

這是正確的:

<body>
<p>This is a paragraph</p>
</body>