Attr.localName

Attr.localName 为只读属性,返回一个属性限定名称的本名部分(去除命名空间前缀的名字)。

在之前的DOM规范中此API被定义在 Node 接口中。

语法

name = attribute.localName

返回值

属性的限定名称的本名 DOMString

示例

下面的例子将弹出一个有“id”文字的警告窗口。

HTML 代码

<button id="example">Click me</button>

JavaScript 代码

const element = document.querySelector("#example");
element.addEventListener("click", function() {
  const attribute = element.attributes[0];
  alert(attribute.localName);
});

注意

本文档中属性的“本名(local name)”指的是属性“限定名称(qualified names)”的命名空间冒号之后的部分。“限定名称”通常作为XML文档命名空间的一部分用在XML代码内。 

注意:在Gecko 1.9.2 以及跟早的版本中,HTML DOM 访问该属性将返回 HTML 节点属性的大写字符串本名(有别于 XML DOM 的 XHTML 属性)。在后来的版本中,为遵循HTML5 标准,该属性返回 DOM 内部存储的名称,即,不论 HTML DOM 的 HTML 属性还是 XML DOM 的 XHTML属性都是小写字符串。

规范

规范 状态 备注
DOM
Attr.localName
Living Standard Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
localNameChrome Full support 46
Notes
Full support 46
Notes
Notes This API was previously available on the Node API.
Edge Full support ≤18Firefox Full support 48
Notes
Full support 48
Notes
Notes This API was previously available on the Node API.
IE No support NoOpera Full support 33
Notes
Full support 33
Notes
Notes This API was previously available on the Node API.
Safari Full support 1.3WebView Android Full support 46
Notes
Full support 46
Notes
Notes This API was previously available on the Node API.
Chrome Android Full support 46
Notes
Full support 46
Notes
Notes This API was previously available on the Node API.
Firefox Android Full support 48
Notes
Full support 48
Notes
Notes This API was previously available on the Node API.
Opera Android Full support 33
Notes
Full support 33
Notes
Notes This API was previously available on the Node API.
Safari iOS Full support 1Samsung Internet Android Full support 5.0
Notes
Full support 5.0
Notes
Notes This API was previously available on the Node API.

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

参考