为什么是< br>一个HTML元素而不是HTML实体? [英] Why is <br> an HTML element rather than an HTML entity?

查看:93
本文介绍了为什么是< br>一个HTML元素而不是HTML实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么呢?不会像& br; 这样的东西更合适吗?

解决方案

根据HTML版本,HTML实体引用是SGML实体或XML实体(HTML从底层技术继承实体)。实体是将其他地方定义的内容块插入到文档中的一种方式。

所有HTML实体都是单字符实体,因此基本上与字符引用相同(技术上它们与字符引用不同,但由于没有定义多字符实体,所以这种区别对HTML没有影响)。例如,当HTML处理器看到时, & mdash; 它根据DTD中的部分用适当的实体替换该实体引用的内容:

 <!ENTITY mdash CDATA&#8212; -  em短跑,U + 2014 ISOpub  - > 

因此它将实体引用替换为实体&#8212; ,这又是一个由字符 - (U + 2014)替换的字符引用。实际上,除非您是通过直接不能理解HTML的通用XML或SGML处理器来实现这一点,否则这将会一步完成。



现在,我们会如何取代你的假设?导致换行发生?我们不能使用换行符,甚至不知名的U + 2028 LINE SEPARATOR(它在语义上以纯文本的含义与< br /> 在HTML中),因为它们是大多数HTML代码中不重要的空白字符,如果我们无法在源代码中对其进行可读性格式化,那么您应该感谢它,因为编写HTML会更困难。 p>

我们需要的不是一个实体,而是一种从语义上表明呈现的内容在这一点上包含换行符的方式。我们也不需要指出任何其他东西(我们已经可以通过开始或结束块元素来指示换行符,但这不是我们想要的)。这样做的唯一合理方法是拥有一个完全意义上的元素,所以我们有< br /> 元素,其相关标签被放入源代码。


Why indeed? Wouldn't something like &br; be more appropriate?

解决方案

An HTML entity reference is, depending on HTML version either an SGML entity or an XML entity (HTML inherits entities from the underlying technology). Entities are a way of inserting chunks of content defined elsewhere into the document.

All HTML entities are single-character entities, and are hence basically the same as character references (technically they are different to character references, but as there are no multi-character entities defined, the distinction has no impact on HTML).

When an HTML processor sees, for example &mdash; it replaces it with the content of that entity reference with the appropriate entity, based on the section in the DTD that says:

<!ENTITY mdash   CDATA "&#8212;" -- em dash, U+2014 ISOpub -->

So it replaces the entity reference with the entity &#8212; which is in turn a character reference that gets replaced by the character (U+2014). In reality unless you are doing this with a general-purpose XML or SGML processor that doesn't understand HTML directly, this will really be done in one step.

Now, what would we replace your hypothetical &br; with to cause a line-break to happen? We can't do so with a newline character, or even the lesser known U+2028 LINE SEPARATOR (which semantically in plain text has the same meaning as <br/> in HTML), because they are whitespace characters which are not significant in most HTML code, which is something that you should be grateful for as writing HTML would be much harder if we couldn't format for readability within the source code.

What we need is not an entity, but a way to indicate semantically that the rendered content contains a line-break at this point. We also need to not indicate anything else (we can already indicate a line-break by beginning or ending a block element, but that's not what we want). The only reasonable way to do so is to have an element that means exactly that, and so we have the <br/> element, with its related tag being put into the source code.

这篇关于为什么是&lt; br&gt;一个HTML元素而不是HTML实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆