Why does the .tagName DOM property return an uppercase value?

别来无恙 提交于 2019-12-18 11:46:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11811426/why-does-the-tagname-dom-property-return-an-uppercase-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!