HTML属性值中的引号?

Kev*_*161 12 html

这似乎是一个真正的基本问题,但......

如何在HTML代码中使用双重语音标记(alt标签等)?

例如..

我正在尝试在我的网页中设置一个标签,Opening Credits for "It's Liverpool"但它限制了它Opening Credits for.

rjz*_*rjz 24

您将需要使用相应的HTML实体代替引号:

<span alt="Opening Credits for &quot;It's Liverpool&quot;">A span</span>
Run Code Online (Sandbox Code Playgroud)


Juk*_*ela 8

您通常可以通过使用适当的语言相关引号而不是Ascii引号来避免此问题,这些引号应限于在计算机代码中用作分隔符.例:

alt="Opening Credits for “It’s Liverpool”"
Run Code Online (Sandbox Code Playgroud)

或(英文)

alt="Opening Credits for ‘It’s Liverpool’"
Run Code Online (Sandbox Code Playgroud)

如果您确实需要在属性值中使用Ascii引号,请使用Ascii撇号作为分隔符:

alt='The statement foo = "bar" is an assignment.'
Run Code Online (Sandbox Code Playgroud)

在非常罕见的情况下,属性值确实需要包含Ascii引号和Ascii撇号,您需要转义它们中的任何一个(即您决定用作属性值分隔符的那个):

alt="The Ascii characters &quot; and ' should not be used in natural languages."
Run Code Online (Sandbox Code Playgroud)

要么

alt='The Ascii characters " and &#39; should not be used in natural languages.'
Run Code Online (Sandbox Code Playgroud)

请注意,这些注意事项仅在属性值内部相关.在元素内容中,"和"都可以自由使用:

<strong>The Ascii characters " and ' should not be used in natural languages.</strong>
Run Code Online (Sandbox Code Playgroud)