📜  html 引用代码 - Html (1)

📅  最后修改于: 2023-12-03 15:01:16.989000             🧑  作者: Mango

HTML 引用代码

HTML 中有多种方式可以引用代码,包括行内代码、块级代码和预格式化文本等。以下是常用的几种方法。

行内代码

行内代码可以使用 <code> 标签表示:

<p>这是一段包含 <code>code</code> 标签的 HTML 代码。</p>

显示效果:

这是一段包含 code 标签的 HTML 代码。

块级代码

块级代码通常用于引用一段较长的代码,可以使用 <pre> 标签表示:

<pre>
  <code>
    function helloWorld() {
      console.log("Hello, World!");
    }
    helloWorld();
  </code>
</pre>

显示效果:

  
    function helloWorld() {
      console.log("Hello, World!");
    }
    helloWorld();
  
预格式化文本

预格式化文本是一种不会自动换行的文本格式,可以使用 <xmp> 标签或 <pre> 标签进行表示,其中 <xmp> 标签已经被废弃,不建议使用:

<xmp>
  function helloWorld() {
    console.log("Hello, World!");
  }
  helloWorld();
</xmp>

<pre>
  function helloWorld() {
    console.log("Hello, World!");
  }
  helloWorld();
</pre>

显示效果:

function helloWorld() { console.log("Hello, World!"); } helloWorld();

  function helloWorld() {
    console.log("Hello, World!");
  }
  helloWorld();

以上是 HTML 中引用代码的几种常用方式。掌握这些方法可以让我们更好地展示代码,提高代码的可读性。