aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/textrange/text/index.html
blob: ae485dd58edf002f76d43b642167e6795eb4553a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: TextRange.text
slug: Web/API/TextRange/text
tags:
  - API
  - DHTML
  - DOM
  - TextRange
---
<div>{{ ApiRef("DOM") }}{{Non-standard_Header}}</div>

<div class="blockIndicator warning">
<p><strong>IE Only</strong></p>
该属性是IE专有的。尽管IE很好地支持它,但大部分其它浏览器已经不支持该属性。该属性仅应在需兼容低版本IE时作为其中一种方案,而不是在跨浏览器的脚本中完全依赖它。</div>

<p>{{domxref("TextRange")}} 接口中的属性 <strong><code>text</code></strong> 用于以 {{domxref("DOMString")}} 形式获取或设置区域内的纯文本内容。该更改直接作用到 DOM 树中,并清除区域内原有的非纯文本元素。注意,该属性忽略所有格式数据,因此若要获取选区中的HTML内容,请使用 {{domxref("TextRange.htmlText")}} 属性。</p>

<h2 id="语法">语法</h2>

<pre>var tS<em>tring</em> = <em>textRange</em>.text;
<em>textRange</em>.text = oString;
</pre>

<h3 id="返回值">返回值</h3>

<p>一个 {{domxref("DOMString")}}</p>

<h2 id="示例">示例</h2>

<p>以下示例在IE9以下有效。该示例通过 <code>document.selection</code> 获取 <code>TextRange</code>,并过滤选区中的富文本元素。IE9以上支持标准的替代方案 {{domxref("Range")}}</p>

<pre class="brush:js">var range = document.selection.createRange();
range.htmlText = range.text;
// 将富文本内容设置为纯文本内容,则区域也就变为纯文本。
</pre>

<h2 id="开发者笔记">开发者笔记</h2>

<h3 id="关于_text_属性">关于 text 属性</h3>

<p>注意,当通过该属性操作或获取时,不会得到包含非纯文本的信息;如果通过该属性设置,则区域内的元素将被删除,之后通常会变为一个包含指定内容的文本节点。因此,即使通过这个属性操作纯文本内容,结果也将剔除原先的所有格式数据。</p>

<p>如果希望脚本的功能明确可读,最好的办法是不要同时使用该属性和 <code>htmlText</code> 属性设置数据。另外,该属性不是标准的,它从IE4开始在IE中实现,但不在其它浏览器的规范中。</p>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div class="hidden">此页上的兼容性表是从结构化数据生成的。如果您想贡献数据,请访问 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并向我们发送一个请求。</div>

<table class="standard-table">
	<thead>
		<tr>
			<th scope="row" style="width: 15px;"></th>
			<th scope="col">IE</th>
			<th scope="col">其它浏览器</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<th scope="row" style="width: 15%;">{{domxref("TextRange.text")}} {{non-standard_inline()}}</th>
			<td>支持(IE9后应使用标准API)</td>
			<td style="width: 60%;">不支持(详见<a href="/zh-CN/docs/Web/API/Selection_API">Selection API</a></td>
		</tr>
	</tbody>
</table>

<h2 id="See_also" name="See_also">扩展</h2>

<ul>
	<li>{{domxref("TextRange")}} 作为该属性的实现接口</li>
	<li>{{domxref("Selection")}}{{domxref("Range")}} 标准接口</li>
	<li><a href="/zh-CN/docs/Web/API/Selection_API">Selection API</a> 用于取代该非标准接口</li>
</ul>