Element.tagName - Web APIs 编辑

The tagName read-only property of the Element interface returns the tag name of the element on which it's called. For example, if the element is an <img>, its tagName property is "IMG" (for HTML documents; it may be cased differently for XML/XHTML documents).

Syntax

elementName = Element.tagName;

Value

A string indicating the element's tag name. This string's capitalization depends on the document type:

  • For DOM trees which represent HTML documents, the returned tag name is always in the canonical upper-case form. For example, tagName called on a <div> element returns "DIV".
  • The tag names of elements in an XML DOM tree are returned in the same case in which they're written in the original XML file. If an XML document includes a tag "<SomeTag>", then the tagName property's value is "SomeTag".

For Element objects, the value of tagName is the same as the value of the nodeName property the element object inherits from Node.

Example

HTML

<span id="born">When I was born...</span>

JavaScript

var span = document.getElementById("born");
console.log(span.tagName);

In XHTML (or any other XML format), the original case will be maintained, so "span" would be output in case the original tag name was created lowercase. In HTML, "SPAN" would be output instead regardless of the case used while creating the original document.

Specifications

SpecificationStatusComment
DOM
The definition of 'Element: tagName' in that specification.
Living Standard 

Browser compatibility

BCD tables only load in the browser

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:126 次

字数:3267

最后编辑:7年前

编辑次数:0 次

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