这是有效的HTML语法吗?

huangapple go评论49阅读模式
英文:

Is this valid HTML syntax?

问题

我花了过去两个小时尝试弄清楚以下代码是否是有效的HTML语法。但至少使用谷歌等搜索引擎没有帮助我得到一个合理的答案。以下是代码:

<p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

请注意 ' 是撇号 ' 的HTML代码。

尽管这个示例似乎被正确渲染,但下一个合乎逻辑的步骤是可以替换其他语法字符,比如括号,这将导致以下代码:

<p style="font-family: 'Segoe UI Adjusted';">Beloha from Hawaii</p>

嗯,这绝对会破坏代码。所以我有点困惑,第一个示例仅替换了撇号是否是有效的HTML语法 - 如果是的话,为什么?

英文:

I have spent the last two hours trying to figure out whether the following code is valid HTML syntax or not. But at least using Google und co didn't help to get a decent answer. Here is the code:

<p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

Please notice the ' which is the HTML code for the apostrophe '.

Although this example seems to get rendered correctly, the next logical step would assume to being able to replace other syntax characters as well, like the brackets, which would lead to the following code:

<p style="font-family: 'Segoe UI Adjusted';">Beloha from Hawaii</p>

Well, this definitely breaks the code. So I'm a little bit confused, whether the first example where just the apostrophes are replaced is valid HTML syntax or not - and if, why?

答案1

得分: 2

你似乎误解了转义的目的。这是一个 img 标签:

<img>

而这是字面文本 <img>

<img>

转义这些字符会阻止它们在标记标签中发挥正常作用,只是显示字面字符而已。

因此,这是一个带有特定样式的 p 标签:

<p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

请注意,你在这里转义了 '。如果我们在属性周围使用了单引号,这是必要的,否则未转义的 ' 将结束属性。但是,在这种情况下,你使用了双引号,所以它没有效果。

这只是字面文本 <p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

<p style="font-family: 'Segoe UI Adjusted';">Beloha from Hawaii</p>

转义的字符会阻止它们呈现为实际的段落,而只会显示特殊字符作为字符。

这两个版本都是完全有效的HTML,只是代表不同的内容。

英文:

You seem to have misunderstood the purpose of escaping. This is a img tag:

<img>

While this is the literal text <img>:

<img>

Escaping the characters stops their normal role in marking tags and just displays the literal character instead.

Thus this is a p tag with a certain style:

<p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

Note you escaped the ' here. If we had used single quotes around the attribute this would be necessary because otherwise a unescaped ' would end the attribute. However, since you used double quotes in this case it has no effect.

This is just the literal text <p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>:

<p style="font-family: 'Segoe UI Adjusted';">Beloha from Hawaii</p>

The escaped characters prevent it from rendering as a actual paragraph and just display the special characters as characters instead.

Both versions are entirely valid HTML, just valid HTML that represents a different thing.

答案2

得分: 1

第二个不是有效的,因为它用于在HTML文件上显示HTML代码。这被称为实体,每个特殊符号都有一个。

假设你想在HTML文件上显示段落的HTML代码。除非你至少将其箭头符号转换为实体,否则无法显示段落。如果你将段落的代码粘贴到HTML文件中,它将显示段落元素,而不是段落元素的代码。因此,实体用于这个目的。

你的第一个代码有效,因为:

<p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

即使你移除了撇号,这段代码也能工作,尽管这是一个不好的做法。

更多信息:MDN文档

英文:

The second one is not valid because it is used to display html code on html file. This is called entities and every special symbol has one.

Suppose, you want to show a html code of paragraph on your HTML file. You cannot show the paragraph until you convert at least it's arrow symbols to entities. If you paste the code of paragraph on your html file, it'll display the paragraph element, not the code of the paragraph element. So, for that purpose entities are used.

Your first code is working because :

<p style="font-family: 'Segoe UI Adjusted';">Aloha from Hawaii</p>

Even if you remove the apostrophe the code will work, although that is a bad practice.

For more info : MDN Docs

huangapple
  • 本文由 发表于 2023年7月20日 12:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定