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/text/assignedslot/index.html | 89 ++++++++++ files/zh-cn/web/api/text/index.html | 195 +++++++++++++++++++++ .../api/text/iselementcontentwhitespace/index.html | 29 +++ .../zh-cn/web/api/text/replacewholetext/index.html | 36 ++++ files/zh-cn/web/api/text/splittext/index.html | 127 ++++++++++++++ files/zh-cn/web/api/text/text/index.html | 96 ++++++++++ files/zh-cn/web/api/text/wholetext/index.html | 141 +++++++++++++++ 7 files changed, 713 insertions(+) create mode 100644 files/zh-cn/web/api/text/assignedslot/index.html create mode 100644 files/zh-cn/web/api/text/index.html create mode 100644 files/zh-cn/web/api/text/iselementcontentwhitespace/index.html create mode 100644 files/zh-cn/web/api/text/replacewholetext/index.html create mode 100644 files/zh-cn/web/api/text/splittext/index.html create mode 100644 files/zh-cn/web/api/text/text/index.html create mode 100644 files/zh-cn/web/api/text/wholetext/index.html (limited to 'files/zh-cn/web/api/text') diff --git a/files/zh-cn/web/api/text/assignedslot/index.html b/files/zh-cn/web/api/text/assignedslot/index.html new file mode 100644 index 0000000000..860697d7bf --- /dev/null +++ b/files/zh-cn/web/api/text/assignedslot/index.html @@ -0,0 +1,89 @@ +--- +title: HTMLSlotElement.assignedSlot +slug: Web/API/Text/assignedSlot +translation_of: Web/API/Text/assignedSlot +--- +

{{SeeCompatTable}}{{APIRef("Shadow DOM")}}

+ +

 assignedSlot 是 {{domxref("Text")}} 接口的属性,返回与该元素相关联的{{domxref("HTMLSlotElement")}} .

+ +

Syntax

+ +
var htmlSlotElement = text.assignedSlot
+ +

Value

+ +

一个 {{domxref("HTMLSlotElement")}} 对象.

+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG','#dom-slotable-assignedslot','assignedSlot')}}{{Spec2('DOM WHATWG')}} 
+ +

Browser Compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(53.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)Firefox OSIE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(53.0)}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}{{CompatChrome(53.0)}}
+
diff --git a/files/zh-cn/web/api/text/index.html b/files/zh-cn/web/api/text/index.html new file mode 100644 index 0000000000..53f9e6ad5a --- /dev/null +++ b/files/zh-cn/web/api/text/index.html @@ -0,0 +1,195 @@ +--- +title: Text +slug: Web/API/Text +translation_of: Web/API/Text +--- +

{{ ApiRef() }}

+

The Text interface represents the textual content of {{domxref("Element")}} or {{domxref("Attr")}}.  If an element has no markup within its content, it has a single child implementing Text that contains the element's text.  However, if the element contains markup, it is parsed into information items and Text nodes that form its children.

+

New documents have a single Text node for each block of text. Over time, more Text nodes may be created as the document's content changes.  The {{domxref("Node.normalize()")}} method merges adjacent Text objects back into a single node for each block of text.

+

属性

+
+
+ {{domxref("Text.isElementContentWhitespace")}} {{readonlyInline}}{{ obsolete_inline() }}
+
+

Returns a {{domxref("Boolean")}} flag indicatingwhether or not the text node contains only whitespace.

+
+
+ {{domxref("Text.wholeText")}} {{readonlyInline}}
+
+ Returns a {{domxref("DOMString")}} containing the text of all Text nodes logically adjacent to this {{domxref("Node")}}, concatenated in document order.
+
+

构造函数

+
+
+ {{domxref("Text.Text", "Text()")}} {{experimental_inline}}
+
+ Returns a Text node with the parameter as its textual content.
+
+

方法

+
+
+ {{domxref("Text.replaceWholeText")}} {{ obsolete_inline() }}
+
+ Replaces the text of the current node and all logically adjacent nodes with the specified text.
+
+
+
+ {{domxref("Text.splitText")}}
+
+ Breaks the node into two nodes at a specified offset.
+
+

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#text', 'Text')}}{{Spec2('DOM WHATWG')}}Removed the isElementContentWhitespace property.
+ Removed the replaceWholeText() method.
+ Added the Text() constructor.
{{SpecName('DOM3 Core', 'core.html#ID-1312295772', 'Text')}}{{Spec2('DOM3 Core')}}Added the isElementContentWhitespace and wholeText properties.
+ Added the replaceWholeText() method.
{{SpecName('DOM2 Core', 'core.html#ID-1312295772', 'Text')}}{{Spec2('DOM2 Core')}}No change from {{SpecName('DOM1')}}.
{{SpecName('DOM1', 'level-one-core.html#ID-1312295772', 'Text')}}{{Spec2('DOM1')}}Initial definition.
+

浏览器兼容性

+

{{CompatibilityTable}}

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0 [3]{{CompatGeckoDesktop("1.0")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}} [3]{{CompatVersionUnknown}} [4]
wholeText1.0{{CompatGeckoDesktop("1.9.1")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
isElementContentWhitespace{{CompatNo}}{{CompatVersionUnknown}}
+ Removed in {{CompatGeckoDesktop("10")}}
{{CompatNo}}{{CompatNo}}{{CompatNo}}
replaceWholeText{{CompatVersionUnknown}} [1] [2]{{CompatGeckoDesktop("1.9.1")}}
+ Removed in {{CompatGeckoDesktop("10")}}
{{CompatVersionUnknown}}{{CompatVersionUnknown}} [1] [2]{{CompatVersionUnknown}} [4]
Text() constructor28.0{{CompatGeckoDesktop("24.0")}}{{CompatNo}}15.0{{CompatNo}}
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}} [3]{{CompatGeckoMobile("1.0")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}} [3]{{CompatVersionUnknown}} [4]
wholeText{{CompatVersionUnknown}}{{CompatGeckoMobile("1.9.1")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
isElementContentWhitespace{{CompatNo}}{{CompatVersionUnknown}}
+ Removed in {{CompatGeckoMobile("10")}}
{{CompatNo}}{{CompatNo}}{{CompatNo}}
replaceWholeText{{CompatVersionUnknown}} [1] [2]{{CompatGeckoMobile("1.9.1")}}
+ Removed in {{CompatGeckoMobile("10")}}
{{CompatVersionUnknown}}{{CompatVersionUnknown}} [1] [2]{{CompatVersionUnknown}} [4]
Text() constructor{{CompatVersionUnknown}}{{CompatGeckoMobile("24.0")}}{{CompatNo}}15.0{{CompatNo}}
+
+

[1] Chromium is currently considering dropping its support.

+

[2] Before Chrome 30 and Opera 17, the argument wasn't mandatory, like required by the specification.

+

[3] Before Chrome 30 and Opera 17, splitText() argument was not mandatory, as required by the specification and implemented by IE and Gecko-based browsers.

+

[4] The argument is not mandatory, though required by the spec.

+

相关链接

+ diff --git a/files/zh-cn/web/api/text/iselementcontentwhitespace/index.html b/files/zh-cn/web/api/text/iselementcontentwhitespace/index.html new file mode 100644 index 0000000000..d138ce0a59 --- /dev/null +++ b/files/zh-cn/web/api/text/iselementcontentwhitespace/index.html @@ -0,0 +1,29 @@ +--- +title: Text.isElementContentWhitespace +slug: Web/API/Text/isElementContentWhitespace +translation_of: Web/API/Text/isElementContentWhitespace +--- +

{{ ApiRef() }}

+

{{ obsolete_header("") }}

+

返回一个布尔值,表明该文本节点的内容是否全部是由空白符组成的.

+
+ 警告: 该属性已经在DOM level 4中被废弃, 在一些新版本的浏览器(Gecko 10.0 )中已经被删除.
+

语法

+
b = textnode.isElementContentWhitespace;
+
+

例子

+

下例中 ,我们创建了一个内容为空白符和文字组成的文本节点.该节点的isElementContentWhitespace属性为false.

+
var tn = document.createTextNode("Hello world");
+tn.isElementContentWhitespace; /* 返回false */
+
+

下例中 ,我们创建了一个内容全部为空白符的文本节点.该节点的isElementContentWhitespace属性为true.

+
var ws = document.createTextNode("  \t \r\n   ")
+ws.isElementContentWhitespace; /* 返回true */
+
+

规范

+

DOM Level 3 Core: Text.isElementContentWhitespace

+

相关链接

+ +

{{ languages( { "en": "en/DOM/Text.isElementContentWhitespace" } ) }}

diff --git a/files/zh-cn/web/api/text/replacewholetext/index.html b/files/zh-cn/web/api/text/replacewholetext/index.html new file mode 100644 index 0000000000..2cfc234e76 --- /dev/null +++ b/files/zh-cn/web/api/text/replacewholetext/index.html @@ -0,0 +1,36 @@ +--- +title: text.replaceWholeText +slug: Web/API/Text/replaceWholeText +translation_of: Web/API/Text/replaceWholeText +--- +

{{ ApiRef() }}

+

{{ Obsolete_header() }}

+

{{ gecko_minversion_header("1.9.1") }}

+

Summary

+

replaceWholeText replaces the text of the node and all of its logically adjacent text nodes with the specified text.  The replaced nodes are removed, including the current node, unless it was the recipient of the replacement text.

+
+ Warning: This method has been deprecated in DOM level 4 and is no longer implemented in recent browsers, including Gecko 10.0 {{ geckoRelease("10.0") }}.
+

Syntax

+
replacementNode = textnode.replaceWholeText(content)
+
+

Parameters

+ +

Exceptions

+

NO_MODIFICATION_ALLOWED_ERR : A DOMException thrown if one of the text nodes being replaced is read only.

+

Example

+

See the example for the text.wholeText property.

+

Notes

+

This method returns the text node which received the replacement text, or null if the replacement text is an empty string.  The returned node is the current node unless the current node is read only, in which case the returned node is a newly created text node of the same type which has been inserted at the location of the replacement.

+
+ Note: Firefox's implementation of this method does not yet support EntityReference nodes as defined by the specification.
+

Specification

+

replaceWholeText

+

See also

+ +

{{ languages( {"en": "en/DOM/text.replaceWholeText" } ) }}

diff --git a/files/zh-cn/web/api/text/splittext/index.html b/files/zh-cn/web/api/text/splittext/index.html new file mode 100644 index 0000000000..4ad321bf85 --- /dev/null +++ b/files/zh-cn/web/api/text/splittext/index.html @@ -0,0 +1,127 @@ +--- +title: Text.splitText() +slug: Web/API/Text/splitText +tags: + - API + - DOM + - Text + - 方法 +translation_of: Web/API/Text/splitText +--- +
{{apiref("DOM")}}
+ +

Text.splitText() 方法可以根据指定的偏移量将一个 {{domxref("Text")}} 节点分割成前后两个独立的兄弟节点。

+ +

如果指定的偏移量刚好等于原文本节点所包含字符串的长度,则返回一个内容为空的文本节点.

+ +

分割后的文本节点还可以使用Node.normalize方法来合并.

+ +

语法

+ +
newNode = textNode.splitText(offset)
+
+ +

参数

+ +
+
offset
+
在中断文本节点前的索引。
+
+ + + +

返回值

+ +

返回一个新创建的 {{domxref("Text")}} 节点,该节点包含了 the text after the specified offset point.

+ +

异常

+ +
+
INDEX_SIZE_ERR
+
如果指定的偏移量小于0或者大于原文本节点中所包含字符串的长度,则抛出这个异常.
+
NO_MODIFICATION_ALLOWED_ERR
+
如果,原文本节点只读,则抛出这个异常.
+
+ +

例子

+ +

下面的例子中,一个 <p> 元素所包含的文本节点将会被分割成两个文本节点,然后在这两个节点中间插入一个 <span> 元素。

+ +

HTML

+ +
  <p id="p">foobar</p>
+
+ +

JavaScript

+ +
const  p = document.getElementById('p');
+
+// 将 <p> 的内容读取为一个文本节点
+const foobar = p.firstChild;
+
+// 将原来的文本节点分割成为内容分别为 foo 和 bar 的两个文本节点
+const bar = foobar.splitText(3);
+
+// 创建一个包含了内容为 ' new content ' 的文本节点的 <u> 元素
+const u = document.createElement('u');
+u.appendChild(document.createTextNode(' new content '));
+
+// 将 <u> 元素插入到后一个文本节点 'bar' 的前面
+p.insertBefore(u, bar);
+
+// 现在,HTML 结构就变成了 <p id="p">foo <span>span contents</span> bar</p>
+
+ +

结果

+ +

{{EmbedLiveSample("Example", 700, 70)}}

+ +

规范

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
规范状态备注
{{SpecName('DOM WHATWG', '#dom-text-splittext', 'Text.splitText')}}{{Spec2('DOM WHATWG')}}No change from {{SpecName('DOM3 Core')}}.
{{SpecName('DOM3 Core', 'core.html#ID-38853C1D', 'Text.splitText')}}{{Spec2('DOM3 Core')}}No change from {{SpecName('DOM2 Core')}}.
{{SpecName('DOM2 Core', 'core.html#ID-38853C1D', 'Text.splitText')}}{{Spec2('DOM2 Core')}}No change from {{SpecName('DOM1')}}.
{{SpecName('DOM1', 'level-one-core.html#ID-38853C1D', 'Text.splitText')}}{{Spec2('DOM1')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.Text.splitText")}}

+ +

参见

+ + + +
+
+
diff --git a/files/zh-cn/web/api/text/text/index.html b/files/zh-cn/web/api/text/text/index.html new file mode 100644 index 0000000000..dee1c9a948 --- /dev/null +++ b/files/zh-cn/web/api/text/text/index.html @@ -0,0 +1,96 @@ +--- +title: Text() +slug: Web/API/Text/Text +tags: + - API + - Text +translation_of: Web/API/Text/Text +--- +

{{ Apiref("DOM")}}{{seeCompatTable}}

+ +

 Text() 构造方法返回一个最新创建的{{domxref("Text")}} 对象,该对象带有可选参数 {{domxref("DOMString")}} 作为文本节点的文本内容(textual content)。

+ +

Syntax

+ +
text1 = new Text(); // 创建一个空的文本节点(text node),即它的textContent 为空字符
+text2 = new Text("This is a text node");//该构造方法参数作为文本节点的textContent 的值。
+
+ +

Example

+ +
text = new Text("Test");
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#text', 'Text.Text()')}}{{Spec2('DOM WHATWG')}}Initial definition.
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support28.0{{CompatGeckoDesktop("24.0")}}{{CompatNo}}15.08.0
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatGeckoMobile("24.0")}}{{CompatNo}}15.08.0
+
+ +

See also

+ + + +

 

diff --git a/files/zh-cn/web/api/text/wholetext/index.html b/files/zh-cn/web/api/text/wholetext/index.html new file mode 100644 index 0000000000..5ec686a5b9 --- /dev/null +++ b/files/zh-cn/web/api/text/wholetext/index.html @@ -0,0 +1,141 @@ +--- +title: Text.wholeText +slug: Web/API/Text/wholeText +translation_of: Web/API/Text/wholeText +--- +

{{ apiref("DOM") }}

+ +

Text.wholeText只读属性返回Text逻辑上相邻的节点的所有文本。文本按文档顺序连接。这允许指定任何文本节点并获取所有相邻文本作为单个字符串。

+ +

Syntax

+ +
str = textnode.wholeText;
+ +

Notes and example

+ +

假设你的网页上有如下的简单文本(包括其中为了格式化代码而添加的一些空格), 其 DOM 节点 被储存在变量 para 中:

+ +
<p>Thru-hiking is great!  <strong>No insipid election coverage!</strong>
+  However, <a href="http://en.wikipedia.org/wiki/Absentee_ballot">casting a
+  ballot</a> is tricky.</p>
+
+ +

你觉得你不喜欢中间的句子, 所以你移除了它:

+ +
para.removeChild(para.childNodes[1]);
+
+ +

过了一会, 你又决定给“Thru-hiking is great, but casting a ballot is tricky.”这句换个说法, 同时保留超链接。 所以你尝试以下代码:

+ +
para.firstChild.data = "Thru-hiking is great, but ";
+
+ +

一切妥当, 是么? 不! 这会使你移除 strong 元素, 而被删掉的句子分隔了两个文本节点. 一个是第一句, 一个是最后一个单词. 相反, 你现在获得如下效果:

+ +
<p>Thru-hiking is great, but However, <a
+  href="http://en.wikipedia.org/wiki/Absentee_ballot">casting a
+  ballot</a> is tricky.</p>
+
+ +

实际上,你更倾向于将这些相邻扽文本节点作为同一文本节点. 这就是 wholeText 的用武之地:如果你有许多相邻的文本节点, 你可以通过wholeText访问这些节点里的所有内容。让我们假设你从未犯过最后一个错误. 在这种情况下, 我们有:

+ +
assert(para.firstChild.wholeText == "Thru-hiking is great!    However, ");
+
+ +

wholeText 只是文本节点的一个属性,特可以返回连接了所有相邻(i.e. 没有被其它元素边界分开) 文本节点数据的字符串 。

+ +

现在让我们回到最初的问题. 我们想做的是用新的文本替代旧的文本. 这就是 {{domxref("Text.replaceWholeText", "replaceWholeText()")}} 用处所在:

+ +
para.firstChild.replaceWholeText("Thru-hiking is great, but ");
+
+ +

我们移除了所有的相邻文本节点 (所有构成whole text的文本节点) 除了调用replaceWholeText() 的,并且把剩余的文本改成了新文本. 我们现在所得到的是这样的:

+ +
<p>Thru-hiking is great, but <a
+  href="http://en.wikipedia.org/wiki/Absentee_ballot">casting a
+  ballot</a> is tricky.</p>
+
+ +

有时候使用whole-text 功能同时使用Node.textContent 或长期支持的 {{domxref("Element.innerHTML")}}; 可以得到更好的处理。如果你需要处理一个元素内的混合内容, 正如本文所介绍的, wholeTextreplaceWholeText() 是有用的。

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM WHATWG', '#dom-text-wholetext', 'Text.wholeText')}}{{Spec2('DOM WHATWG')}}No significant change.
{{SpecName('DOM3 Core', 'core.html#Text3-wholeText', 'Text.wholeText')}}{{Spec2('DOM3 Core')}}Initial definition.
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0{{CompatVersionUnknown}}{{CompatGeckoDesktop("1.9.1")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("1.9.1")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

See also

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