首页 > 解决方案 > 什么样的文本可以包含在 HTML 代码标签中?

问题描述

我想知道哪种文本属于 HTML<code>元素,哪些不属于?

例如,我知道这是 HTML<code>标签的一个很好的用法:

Use the <code>str()</code> function to convert the object into a string.

但我不确定这些是否是<code>标签的好用法:

1. The list of users can be found at <code>/etc/passwd</code>.
2. We need to wait for <code>200 OK</code> response before the next step.
3. Enter the <code>ls</code> command to obtain a directory listing.
4. Compile the source code in <code>foo.c</code> to <code>foo.o</code>.

是否有标准文档或 W3C 指南文档或类似的权威参考来精确定义哪些元素可能属于 HTML<code>元素,哪些可能不属于?

标签: htmlstandardsw3csemantic-markup

解决方案


code元素的定义(来自 HTML 5.2)是:

code元素代表一段计算机代码。这可以是 XML 元素名称、文件名、计算机程序或计算机可以识别的任何其他字符串。

这决定了是否允许(即语义)使用该元素。但是您还应该检查是否有更具体的元素可用。

查看您的示例

Use the <code>str()</code> function to convert the object into a string.

这可以。

The list of users can be found at <code>/etc/passwd</code>.

这可以。

We need to wait for <code>200 OK</code> response before the next step.

您可以考虑改用该samp元素,它表示“来自另一个程序或计算系统的样本或引用输出”。

Enter the <code>ls</code> command to obtain a directory listing.

您可以考虑改用该kbd元素,它表示“用户输入(通常是键盘输入,[...])”。

Compile the source code in <code>foo.c</code> to <code>foo.o</code>.

这可以。


推荐阅读