Why does the .tagName DOM property return an upper

2019-02-03 03:35发布

问题:

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.