HTML属性值中的引号?

16

这似乎是一个非常基础的问题,但是...

在 HTML 代码中如何使用双引号(例如 alt 标签等)?

比如说...

我正在尝试在我的网页上设置一个标签为 Opening Credits for "It's Liverpool",但它只显示为 Opening Credits for


alt是图像元素的属性,而不是标签。 - Soup
2个回答

29

您需要使用相应的HTML实体来替换引号:

<span alt="Opening Credits for &quot;It's Liverpool&quot;">A span</span>

9

您可以通过使用适当的依赖于语言的引号来避免此问题,而不是Ascii引号,后者应仅在计算机代码中用作分隔符。例如:

alt="Opening Credits for “It’s Liverpool”"

或者(在英式英语中)
alt="Opening Credits for ‘It’s Liverpool’"

如果确实需要在属性值中使用ASCII引号,请使用ASCII撇号作为分隔符:
alt='The statement foo = "bar" is an assignment.'

在极其罕见的情况下,如果属性值确实需要同时包含Ascii引号和Ascii撇号,则需要对它们进行转义(即您决定用作属性值分隔符的那个字符):

alt="The Ascii characters &quot; and ' should not be used in natural languages."

或者

alt='The Ascii characters " and &#39; should not be used in natural languages.'

请注意,这些考虑因素仅适用于属性值内部。在元素内容中,"和'都可以自由使用:
<strong>The Ascii characters " and ' should not be used in natural languages.</strong>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接