为什么.tagName DOM属性返回一个大写值? [英] Why does the .tagName DOM property return an uppercase value?

查看:588
本文介绍了为什么.tagName DOM属性返回一个大写值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我们有

 < html> 
< head>
< title> FooBar< / title>
< / head>
< body>< / body>
< / html>

如果我们执行 document.getElementByTagName(title)。TagName ,那么我们将有 TITLE (大写)。虽然html标准建议以小写形式写html标签。



我知道两者之间没有关系,但这仍然没有意义。



是否有任何理由让DOM以大写形式返回标记名?

解决方案

从技术上讲,在 DOM级别1


HTML DOM返回HTML元素的 tagName 无论源HTML文档中的情况如何,都是规范的大写形式。

大写标记名称的约定可能源于遗留问题,此前HTML基于SGML开发,元素类型声明为大写。请参阅讨论SGML,HTML及其语法的HTML 4.01规范的这一部分 ,以及例如 HTML 4.01严格的文档类型定义。任何支持HTML的DOM实现都可以遵循。



请注意,只有在XHTML中明确需要小写标签名称(而不是XML),并且通常建议作者编写小写标签便于在HTML / XHTML之间移植,以及提高可读性。但是,这个建议不会在规范中发生;它所说的只是HTML标记名称不区分大小写,而不是XHTML和XML。


For example, if we have

<html>
    <head>
        <title>FooBar</title>
    </head>
    <body></body>
</html>

If we do document.getElementByTagName("title").TagName, then we will have TITLE (uppercase). While the html standards recommends writing html tags in lowercase.

I know there is no relationship between both, but this still doesn't make sense.

Is there any reason that DOM should return tag names in uppercase?

解决方案

Technically, this is mandated in DOM Level 1:

The HTML DOM returns the tagName of an HTML element in the canonical uppercase form, regardless of the case in the source HTML document.

The convention of uppercase tag names probably stems from legacy, when HTML was previously developed based on SGML, and element types were declared in uppercase. See this section of the HTML 4.01 spec discussing SGML, HTML and its syntax, as well as for example the HTML 4.01 Strict doctype definition. Any DOM implementations supporting HTML would follow suit.

Note that lowercase tag names are only explicitly required in XHTML (but not XML), and authors are generally recommended to write lowercase tags for easy porting between HTML/XHTML, as well as improving readability. However, this recommendation doesn't occur in the spec; all it says is that tag names are case-insensitive only in HTML as opposed to XHTML and XML.

这篇关于为什么.tagName DOM属性返回一个大写值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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