使用 HTML 文档类型以 XHTML 编写网页的一部分有什么问题吗?

发布于 2024-07-24 23:15:43 字数 949 浏览 13 评论 0原文

我最近刚刚开始学习 HTML/CSS,并且一直在尝试自学良好的 Web 编程实践。 我熟悉 XML,因此用 XHTML 编写网页非常直观,而且看起来是一件不错的事情。 但是,我一直在阅读类似 这个 现在我很矛盾。

我对 HTML 和 XHTML 编码的担忧源于以下原因:

  1. img 标签不需要在 HTML 中封闭,这对我来说是有意义的。 但在 XHTML 中,img 标签需要一个关闭标签,这看起来有点时髦。 我只是觉得有一对打开和关闭标签而中间没有任何内容很奇怪。
  2. p 标签(如段落中)不需要在 HTML 中关闭,这对我来说很时髦。 像在 XHTML 中一样,在开始标记和结束标记之间划分段落是有意义的。

这些是我目前能想到的唯一基本示例,但我希望它们至少能表明我对 HTML 和 XHTML 的分歧程度。 所以这是我的实际问题:当我编写应该采用 HTML(带有 HTML DTD)的网页时,是否可以添加 XHTML 代码(当我觉得它适当地澄清了某些内容时)? 我确信网站看起来是一样的,但是如果人们查看我的来源,他们会认为我是一个非常不专业的编码员吗?

另外,我一直在盲目地通过谷歌浏览关于健全的网络编程实践的教程和指南,我想知道你们是否有任何建议/资源可以与我分享。 我真的想确保我正在学习如何以正确的方式做到这一点。 我真的很感激。

谢谢阅读!

编辑:哇,反应这么快; 你们太棒了! 所以我知道网络浏览器在处理这类事情时会非常宽松。 我的问题是虚荣心以及其他专业人士对编写 XHTML 和 HTML 的混合体有何感受。 严格遵守代码是正确的选择吗? 老实说,我只是不想看起来像个白痴,哈哈。

I've just recently started learning HTML/CSS and I've been trying to teach myself sound web programming practices. I'm familiar with XML, so coding up webpages in XHTML was intuitive enough and it just seemed like a sound thing to do. However, I've been reading articles like this one
and now I'm ambivalent.

My concerns with coding in either HTML and XHTML stem from the following:

  1. img tags do not need to be closed in HTML, and that makes sense to me. But in XHTML, img tags require a close tag which seems kind of funky. I just think it's weird to have a pair of open and close tags with nothing in between.
  2. p tags (as in paragraph) do not need to be closed in HTML, and that is funky to me. It makes sense to section off a paragraph between open and close tags as you would in XHTML.

These are the only basic examples I can come up with at the moment, but I'm hoping that they at least show how split I am between HTML and XHTML. So here's my actual question: Is it okay to throw in XHTML code (when I feel that it appropriately clarifies something) when I'm coding up a webpage that should be in HTML (with an HTML DTD)? I'm sure the website would look the same, but if people were to look at my source, would they think I were a terribly unprofessional coder?

Also, I've been sort of blindly looking through tutorials and guides via Google on sound web programming practices and I was wondering if you folks had any advice/resources to share with me. I really want to make sure I'm learning how to do this the right way. I would really appreciate it.

Thanks for reading!

Edit: Wow, such fast responses; you guys are awesome! So I understand that web browsers are going to be pretty lenient when it comes to this sort of stuff. My problem is then a question of vanity and how other professionals feel about writing a sort of mix of XHTML and HTML. Is strictly compliant code the way to go? I honestly just don't want to look like an idiot, haha.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城挽邺 2024-07-31 23:15:44

不,这不行。 至少在理论上是这样。 如果您在 HTML 文档中编写 XHTML 语法,则它是无效的。 浏览器很可能会以相同的方式呈现它,但它仍然无效。 另外,唯一在 XHTML 中有效而在 HTML 中无效的是自闭合标记 (
)。 只是因为在 HTML 中您不需要关闭标签,但这并不意味着您不能关闭它们。 您可以这样编写标记:

Lorem ipsum dolor sat amet, consectetur adipiscing elit。 现在在 enim 坐 amet urna placerat porttitor ut quis justo。 Quisque posuere nisl ac libero dictum posuere。 悬而未决。 多内克和智人前庭。 Lorem ipsum dolor sat amet,consectetur adipiscing elit。 Sed at nulla dui。

Lorem ipsum dolor sat amet,consectetur adipiscing elit 。 Praesent at enim sat amet urna placerat porttitor ut quis justo。

看到了吗? 它是有效的 HTML,而且看起来不错。

顺便说一句:阅读此内容

No, it's not okay. In theory, at least. If you write XHTML syntax in a HTML document, it's invalid. Browsers will most likely render it the same way, but it's still invalid. Plus, the only thing valid in XHTML and invalid in HTML is a self-closing tag (<br />). Just beacuse in HTML you're not required to close tags, doesn't mean you can't close them. You can write markup like this:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent at enim sit amet urna placerat porttitor ut quis justo. Quisque posuere nisl ac libero dictum posuere. Suspendisse in magna neque. Donec et vestibulum sapien. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at nulla dui.</p>

<img src="" alt="">

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent at enim sit amet urna placerat porttitor ut quis justo.</p>

See? It's valid HTML, and looks good.

BTW: read this.

濫情▎り 2024-07-31 23:15:44

是 SGML 的遗留物,只能在文档顶部出现一次。 在文档中添加其他 DOCTYPE 声明将违反 HTML 和 XHTML — 尽管大多数浏览器可能会原谅您。

如果必须在页面中同时使用两者,也许将嵌套文档放入 IFRAME 中会更符合标准,因为 IFRAME 根据定义包含单独的窗口/文档上下文。

<DOCTYPE> is a leave-over from SGML and should only appear once, at the top of the document. Adding other DOCTYPE declarations within the document would be a violation of both HTML and XHTML--though most browsers would probably forgive you.

If you must use both in a page, perhaps placing the nested document into an IFRAME would be more consistent with standards since IFRAMEs by definition house a separate window/document context.

べ繥欢鉨o。 2024-07-31 23:15:43

HTML 5 允许使用 XHTML 语法。

您现在可以开始使用 HTML 5 文档类型,这会将所有浏览器推入标准模式,但请注意,仅某些浏览器支持 HTML 5 的新功能。

来自网络超文本应用技术工作组

我应该用/>关闭空元素吗? 或>?

HTML 中的空元素(例如 br、img 和 input 元素)不需要尾部斜杠。 例如,您只需编写
,而不需要编写
。 这与 HTML 4.01 中的相同。 然而,由于广泛尝试使用 XHTML 1.0,有大量页面使用尾部斜杠。 因此,HTML 中的 void 元素允许使用尾部斜杠语法,以简化从 XHTML 1.0 到 HTML5 的迁移。

非常简单的 HTML 5 文档类型如下所示:

<!DOCTYPE html>

所以是的,您可以使用自结束标记编写符合标准的 HTML,它将根据 HTML 5 验证器。 业界广泛选择采用 HTML5 而不是 XHTML2,因此我不建议在任何新项目中使用 XHTML。


编辑:需要澄清的是,现在使用 HTML 5 文档类型是安全的,并且可以安全地假设在行业支持下 HTML 5 将超过 XHTML 2 被广泛采用。 您现在当然可以编写 HTML 5 文档,但是 HTML5 的许多功能,例如 (特别是缺乏 MS 的支持,因为它与 Silverlight 竞争)没有跨浏览器支持,并且可能会发生变化。

HTML 5 allows for the use of XHTML syntax.

You can start using an HTML 5 doctype now which will push all browsers into standards mode, but be aware that new features of HTML 5 are only supported in some browsers.

From the Web Hypertext Application Technology Working Group:

Should I close empty elements with /> or >?

Void elements in HTML (e.g. the br, img and input elements) do not require a trailing slash. e.g. Instead of writing <br />, you only need to write <br>. This is the same as in HTML 4.01. However, due to the widespread attempts to use XHTML 1.0, there are a significant number of pages using the trailing slash. Because of this, the trailing slash syntax has been permitted on void elements in HTML in order to ease migration from XHTML 1.0 to HTML5.

The marvellously simple HTML 5 doctype looks like this:

<!DOCTYPE html>

So yes, you can write standards compliant HTML with self closing tags and it will validate against an HTML 5 validator. The industry has widely chosen to adopt HTML5 over XHTML2, and consequently I wouldn't recommend using XHTML for any new projects.


Edit: Just as a point of clarification, it is safe to use the HTML 5 doctype now, and it is also safe to assume that HTML 5 will be widely adopted over XHTML 2 given industry support. You can certainly author HTML 5 documents now, however many of the features of HTML5, such as <canvas> (notably lacking support from MS given that it competes with Silverlight) do not have cross browser support and are subject to change.

仅此而已 2024-07-31 23:15:43

img标签不需要在HTML中关闭,
这对我来说很有意义。

不需要关闭标签。 所有元素都需要关闭。

大多数元素由开始标记、一些内容和结束标记组成。

由于图像元素不能有内容,因此不需要结束标记。 在 HTML 中,不能包含内容的元素(“空元素”)禁止使用结束标记。

但是在 XHTML 中,img 标签需要一个关闭标签,这看起来有点时髦。

XHTML 是一种 XML 应用程序,而不是 SGML 应用程序,并且 XML 不具备 SGML 所具备的所有功能 — 包括指定禁止​​使用结束标记的能力。

我只是觉得有一对打开和关闭标签而中间没有任何内容很奇怪。

XML 引入了一种新的标记 — 自闭合标记 (),它充当开始标记和结束标记的组合。

如果您正在编写 HTML 兼容的 XHTML(如果您选择使用 XHTML 并希望支持不支持该语言的浏览器,例如 Internet Explorer,则应该这样做),那么您 应该使用该语法。 (我自己更喜欢坚持使用 HTML)

p 标签(如段落中)不需要在 HTML 中关闭,这对我来说很有趣。

HTML 中的许多元素都有可选结束标记。 这允许在可以隐含结束标记时将其省略。

例如:

<p>Hello, world
<p>A second paragraph

由于 ap 元素不能包含另一个 p 元素,因此您可以暗示(100% 可靠)开始一个新段落将结束前一个段落。

也就是说,它们是可选,而不是禁止的。 您可以为任何本质上不为空的元素编写具有显式结束标记的 HTML 4.01。

可以添加XHTML代码吗
(当我觉得合适的时候
澄清了一些事情)当我编码时
建立一个应该是 HTML 的网页
(使用 HTML DTD)? 我确信
网站看起来是一样的,但是如果
人们要查看我的消息来源,
他们会认为我是一个可怕的人吗?
不专业的编码员?

那样会显得不专业。

也就是说,XHT​​ML 语法与 HTML 语法不符的情况只有三种:

空元素 — 例如
需要在 XHTML 中显式关闭...但您说您不喜欢这种语法。

脚本和样式 - 如果您在 XHTML 中的嵌入式 JS 和 CSS 中使用某些字符,则需要使用字符引用或 CDATA 标记,但这些元素在 HTML 的 DTD 中标记为 CDATA,因此这会那里是错误的......但无论如何你不应该使用嵌入式脚本或样式。

命名空间——但是在 HTML 文档中使用 XML 命名空间根本无法澄清某些事情。

img tags do not need to be closed in HTML,
and that makes sense to me.

No tag needs to be closed. All elements need to be closed.

Most elements consist of a start tag, some content, and an end tag.

Since an image element cannot have content, the end tag is not needed. In HTML, elements which cannot have content ("EMPTY elements") have the end tags forbidden.

But in XHTML, img tags require a close tag which seems kind of funky.

XHTML is an XML application rather than an SGML application, and XML doesn't have all the features that SGML does — including the ability to specify that an end tag is forbidden.

I just think it's weird to have a pair of open and close tags with nothing in between.

XML introduces a new kind of tag — a self-closing tag (<foo />) which acts as a start and end tag combined.

If you are writing HTML-Compatible XHTML (which you should be if you choose to use XHTML and want to support browsers which don't support that language — such as Internet Explorer), then you should use that syntax. (I prefer to stick to HTML myself)

p tags (as in paragraph) do not need to be closed in HTML, and that is funky to me.

Many elements in HTML have optional end tags. This allows the end tag to be omitted when it can be implied.

For example:

<p>Hello, world
<p>A second paragraph

Since a p element cannot contain another p element, you can imply (with 100% reliability) that starting a new paragraph will close the previous one.

That said, they are optional, not forbidden. You can write HTML 4.01 with explicit end tags for any element which isn't intrinsically empty.

Is it okay to throw in XHTML code
(when I feel that it appropriately
clarifies something) when I'm coding
up a webpage that should be in HTML
(with an HTML DTD)? I'm sure the
website would look the same, but if
people were to look at my source,
would they think I were a terribly
unprofessional coder?

It would look unprofessional.

That said, there are only three times when XHTML syntax is not conforming HTML syntax:

Empty elements — such as <img> and <br> need to be explicitly closed in XHTML … but you said you didn't like that syntax.

Scripts and styles — you need to use character references or CDATA markers if you use certain characters in your embedded JS and CSS in XHTML, but these elements are marked as CDATA in the DTD for HTML, so this would be wrong there … but you shouldn't use embedded scripts or styles anyway.

Namespaces — but there is no time when using an XML namespace in the middle of an HTML document would come close to clarifying something.

‘画卷フ 2024-07-31 23:15:43

从几乎每个浏览器都会正确呈现它的意义上来说,它是“好的”,但这只是因为它们是为处理格式错误的 HTML/XHTML/XML/其他内容而构建的。 但是,任何与声明的文档类型(在您的情况下为 HTML DTD)不匹配的元素都将阻止您的代码验证,从技术上讲,它是格式错误的。

另一方面,绝大多数网站(即使是正确呈现的网站)都没有验证,所以这不是一个大问题。 我会坚持(严格)XHTML; 请注意 jimr 关于自闭合标签的评论(例如 )。

It is 'okay' in the sense that almost every browser will render it correctly, but only because they're built to handle malformed HTML/XHTML/XML/whatever. However, any element(s) not matching the declared doctype (in your case, the HTML DTD) will prevent your code from validating, and it is, technically, malformed.

On the other hand, the vast majority of websites (even ones that render correctly) do not validate, so it's not a huge issue. I would stick to (strict) XHTML; note jimr's comment concerning self-closing tags (e.g. <img src="foo" />).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文