From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/attr/index.html | 238 +++++++++++++++++++++++ files/zh-cn/web/api/attr/localname/index.html | 82 ++++++++ files/zh-cn/web/api/attr/namespaceuri/index.html | 121 ++++++++++++ files/zh-cn/web/api/attr/prefix/index.html | 67 +++++++ 4 files changed, 508 insertions(+) create mode 100644 files/zh-cn/web/api/attr/index.html create mode 100644 files/zh-cn/web/api/attr/localname/index.html create mode 100644 files/zh-cn/web/api/attr/namespaceuri/index.html create mode 100644 files/zh-cn/web/api/attr/prefix/index.html (limited to 'files/zh-cn/web/api/attr') diff --git a/files/zh-cn/web/api/attr/index.html b/files/zh-cn/web/api/attr/index.html new file mode 100644 index 0000000000..a87717916d --- /dev/null +++ b/files/zh-cn/web/api/attr/index.html @@ -0,0 +1,238 @@ +--- +title: Attr +slug: Web/API/Attr +tags: + - 应用接口 + - 文档对象模型 + - 浏览器兼容性 +translation_of: Web/API/Attr +--- +
{{APIRef("DOM")}}
+ +

该类型使用对象来表示一个DOM元素的属性。在大多数DOM方法中,你可能会直接通过字符串的方式获取属性值(例如{{domxref("Element.getAttribute()")}}),但是一些函数(例如{{domxref("Element.getAttributeNode()")}})或通过迭代器访问时则返回Attr类型。

+ +

{{InheritanceDiagram}}

+ +
警告:从Gecko 7.0开始{{geckoRelease("7.0")}},控制台会输出这些方法和属性将会被移除的警告信息。你应该对代码进行相应的修正。点击{{anch("Deprecated properties and methods")}}查看完整的列表。
+ +
DOM4[REC]中,为了规范化Attr的实现,它不再继承自{{domxref("Node")}}。在目前DOM4.1[WD]中又有变动,因此不建议使用Attr对象上有关{{domxref("Node")}}的属性和方法。
+ +

属性

+ +
+
{{domxref("Attr.name", "name")}} {{readOnlyInline}}
+
该属性的名称
+
{{domxref("Attr.namespaceURI", "namespaceURI")}} {{readOnlyInline}}
+
+

表示该属性的命名空间URI{{domxref("DOMString")}},如果该元素不在命名空间中,则返回null。

+
+
{{domxref("Attr.localName", "localName")}} {{readOnlyInline}}
+
+

表示该属性的命名空间限定的本地名称{{domxref("DOMString")}}。

+
+
{{domxref("Attr.prefix", "prefix")}} {{readOnlyInline}}
+
表示该属性的命名空间前缀{{domxref("DOMString")}},如果没有前缀指定则返回null。
+
{{domxref("Attr.ownerElement", "ownerElement")}} {{readOnlyInline}}
+
+

该属性所附属的元素节点。

+ +
+

注意: DOM Level 4移除了这个方法。由于当你从{{domxref("Element")}}中获得Attr对象时,你应已知相关的元素。
+ 在某些场景下并一定能够得到相关的元素,比如通过{{domxref("Document.evaluate")}}返回的Attr对象,最新的DOM草案再次引入该属性。

+ +

Gecko从Gecko 7.0 {{geckoRelease("7.0")}}开始会输出一个废弃的提示信息。 该提示信息在Gecko 49.0 {{geckoRelease("49.0")}}再次被删除。

+
+
+
{{domxref("Attr.specified", "specified")}} {{readOnlyInline}}
+
该属性将返回。如果这个属性你在源代码或者在脚本中明确指定的话,它总是返回真。否则它是由文档的DTD默认定义的,将总是返回
+
{{domxref("Attr.value", "value")}}
+
属性的值
+
+ +
+

注意: DOM Level 3定义namespaceURI, localNameprefix为{{domxref("Node")}}接口。在DOM4中被移至Attr

+ +

Chrome 46.0版本以上、Firefox 48.0版本以上实现了该改动。

+
+ +

废弃的属性和方法

+ +

这些属性已经被废弃,可以使用合适的属性替代。

+ +
+
attributes
+
+

当前该属性总是返回 NULL

+
+
childNodes {{obsolete_inline(14)}}
+
当前该属性总是返回一个空的 {{domxref("NodeList")}}.
+
firstChild {{obsolete_inline(14)}}
+
当前该属性总是返回NULL
+
isId {{readOnlyInline}}
+
表明该属性是否一个“ID 属性”。“ID 属性”的值在整个DOM文档中应当是唯一。在HTML DOM文档中属性“id”是一个ID属性,也是唯一一个ID属性;但是在XML文档中可以定义其他ID属性。一个属性是否是唯一的,通常由{{Glossary("DTD")}}或其他文档模式描述文件决定。
+
lastChild
+
当前该属性总是返回NULL
+
nextSibling
+
当前该属性总是返回NULL
+
nodeName
+
使用{{domxref("Attr.name")}}来代替
+
nodeType
+
当前该属性总是返回2,表示ATTRIBUTE_NODE
+
nodeValue
+
使用{{domxref("Attr.value")}}来代替
+
ownerDocument
+
这个属性本不应当在这里被使用,所以应该无须担心其演变
+
parentNode
+
当前该属性总是返回NULL
+
previousSibling
+
当前该属性总是返回NULL
+
schemaTypeInfo {{obsolete_inline}} {{readOnlyInline}}
+
当前属性的类型信息。然而当加载完文档完或调用{{domxref("Document.normalizeDocument")}}后,这个被认定为绝对正确的包含在节点内的类型信息,会因为节点的移动而变得不可信。
+
specified
+
当前该属性总是返回true
+
textContent
+
使用{{domxref("Attr.value")}}来代替
+
+ +

这些方法已经被废弃:

+ +
+
appendChild(){{obsolete_inline(14)}}
+
通过编辑{{domxref("Attr.value")}}属性来实现相同的效果
+
cloneNode()
+
这个方法本不应当在这里被使用,所以无须担心其演变
+
createAttribute()
+
使用{{domxref("Element.setAttribute()")}}来代替
+
createAttributeNS()
+
使用{{domxref("Element.setAttributeNS()")}}来代替
+
getAttributeNode()
+
使用{{domxref("Element.getAttribute()")}}来代替
+
getAttributeNodeNS()
+
使用{{domxref("Element.getAttributeNS()")}}来代替
+
hasAttributes() {{obsolete_inline("21.0")}}
+
当前该方法总是返回false.
+
hasChildNodes()
+
当前该方法总是返回false.
+
insertBefore()
+
通过编辑{{domxref("Attr.value")}}来实现相同效果
+
isSupported()
+
这个方法本不应当被在这里使用,所以无须担心其演变
+
isEqualNode()
+
这个方法本不应当被在这里使用,所以无须担心其演变
+
normalize()
+
这个方法本不应当被在这里使用,所以无须担心其演变
+
removeAttributeNode()
+
使用{{domxref("Element.removeAttribute()")}}来代替
+
removeChild(){{obsolete_inline(14)}}
+
通过编辑{{domxref("Attr.value")}}来实现相同效果
+
replaceChild(){{obsolete_inline(14)}}
+
通过编辑{{domxref("Attr.value")}}来实现相同效果
+
setAttributeNode()
+
使用{{domxref("Element.setAttribute()")}}来代替
+
setAttributeNodeNS()
+
使用{{domxref("Element.setAttributeNS()")}}来代替
+
+ +

规格

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规格状态注释
{{SpecName("DOM WHATWG", "#interface-attr", "Attr")}}{{Spec2("DOM WHATWG")}}加回 ownerElement 属性
{{SpecName("DOM4", "#interface-attr", "Attr")}}{{Spec2("DOM4")}}namespaceURI、prefixlocalName从 {{domxref("Node")}} 移至本API,且删除 ownerElement、schemaTypeInfoisId.
{{SpecName("DOM3 Core", "core.html#ID-637646024", "Attr")}}{{Spec2("DOM3 Core")}} +

扩展 schemaTypeInfo, isId

+
Document Object Model (DOM) Level 1 Core Specification
+ Attr
Obsolete初始定义
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
基本支持{{CompatVersionUnknown}}[1]{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
基本支持{{CompatVersionUnknown}}{{CompatVersionUnknown}}[1]{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}[1]
+
+ +

[1] As of Chrome 45, this property no longer inherits from Node.

+ +

参考

+ + diff --git a/files/zh-cn/web/api/attr/localname/index.html b/files/zh-cn/web/api/attr/localname/index.html new file mode 100644 index 0000000000..352e0aa02f --- /dev/null +++ b/files/zh-cn/web/api/attr/localname/index.html @@ -0,0 +1,82 @@ +--- +title: Attr.localName +slug: Web/API/Attr/localName +translation_of: Web/API/Attr/localName +--- +
{{APIRef("DOM")}}
+ +

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

+ +
+

在之前的DOM规范中此API被定义在 {{domxref("Node")}} 接口中。

+
+ +

语法

+ +
name = attribute.localName
+
+ +

返回值

+ +

属性的限定名称的本名 {{domxref("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);
+});
+
+ +

{{ EmbedLiveSample('Example','100%',30) }}

+ +

注意

+ +

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

+ +
+

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

+
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('DOM WHATWG', '#dom-attr-localname', 'Attr.localName')}}{{Spec2('DOM WHATWG')}}Initial definition
+ +

浏览器兼容性

+ + + +

{{Compat("api.Attr.localName")}}

+ +

参考

+ + diff --git a/files/zh-cn/web/api/attr/namespaceuri/index.html b/files/zh-cn/web/api/attr/namespaceuri/index.html new file mode 100644 index 0000000000..eab2e0094c --- /dev/null +++ b/files/zh-cn/web/api/attr/namespaceuri/index.html @@ -0,0 +1,121 @@ +--- +title: Attr.namespaceURI +slug: Web/API/Attr/namespaceURI +translation_of: Web/API/Attr/namespaceURI +--- +
{{APIRef("DOM")}}
+ +

Attr.namespaceURI 只读属性返回属性的命名空间URI,如果该元素不在命名空间中,则返回null。

+ +
+

在DOM4之前,该API是在 {{domxref("Node")}} 中定义的。

+
+ +

语法

+ +
namespace = attribute.namespaceURI
+ +

例子

+ +

在这个片段中,正在检查一个属性的 {{domxref("localName")}} 和 namespaceURI. 如果 namespaceURI 返回XUL命名空间,并且localName返回 "browser",则该节点被理解为XUL  <browser/>

+ +
if (attribute.localName == "value" &&
+    attribute.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul") {
+  // this is a XUL value
+}
+ +

笔记

+ +

这不是基于对范围中的命名空间声明的检查的命名空间查找的结果的计算值。
+ 属性的命名空间URI在属性创建时被冻结。

+ +

在Firefox 3.5及更早版本中,HTML文档中HTML属性的命名空间URI为null。
+ 在后来的版本中,遵照HTML5,就像在XHTML中一样是https://www.w3.org/1999/xhtml{{gecko_minversion_inline("1.9.2")}}

+ +

您可以使用DOM Level 2方法 {{domxref("Element.setAttributeNS")}}创建具有指定namespaceURI的属性。

+ +

根据 Namespaces in XML 规范, 属性不会从其附加的元素继承其命名空间。
+ 如果一个属性没有明确地给出一个命名空间,它没有命名空间。

+ +

DOM本身不处理或强制命名空间验证。 DOM应用程序需要做任何必要的验证。
+ 还要注意,一旦与特定节点相关联,命名空间前缀将无法更改。

+ +

规格

+ + + + + + + + + + + + + + + + +
规格状态注释
{{SpecName("DOM4", "#dom-element-namespaceuri", "Element.namespaceuri")}}{{Spec2("DOM4")}}初始定义
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
特征ChromeFirefox (Gecko)Internet ExplorerOperaSafari
基本支持46.0[1]{{CompatGeckoDesktop("48.0")}}[1]{{CompatUnknown}}{{CompatVersionUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
特征AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基本支持{{CompatUnknown}}{{CompatGeckoMobile("48.0")}}[1]{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] 此API以前在 {{domxref("Node")}} API上可用。

+ +

其他文章

+ + diff --git a/files/zh-cn/web/api/attr/prefix/index.html b/files/zh-cn/web/api/attr/prefix/index.html new file mode 100644 index 0000000000..16215636ec --- /dev/null +++ b/files/zh-cn/web/api/attr/prefix/index.html @@ -0,0 +1,67 @@ +--- +title: Attr.prefix +slug: Web/API/Attr/prefix +translation_of: Web/API/Attr/prefix +--- +
{{APIRef("DOM")}}
+ +

Attr.prefix 为只读属性,返回指定标签属性的名字空间前缀,如果没有前缀则返回 null

+ +
+

在 DOM4 之前此 API 被定义在 {{domxref("Node")}} 接口中。

+
+ +

语法

+ +
string = attribute.prefix
+
+ +

示例

+ +

下例在控制台中输出“x”。

+ +
<div x:id="example" onclick="console.log(this.attributes[0].prefix)"/>
+
+ +

注意

+ +

该属性仅在使用有名字空间解析功能的解析器时有效,例如一个MIME类型为XML的文档。在HTML文档中无效。

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('DOM WHATWG', '#dom-attr-prefix', 'Attr: prefix')}}{{Spec2('DOM WHATWG')}}
{{SpecName("DOM4", "#dom-attr-prefix", "Attr.prefix")}}{{Spec2("DOM4")}}
+ +

浏览器兼容性

+ + + +

{{Compat("api.Attr.prefix")}}

+ +

参考

+ + -- cgit v1.2.3-54-g00ecf