textContent
特性为
节点
interface represents the text content of the node and its descendants.
注意:
textContent
and
HTMLElement.innerText
are easily confused, but the two properties are
different in important ways
.
let text = someNode.textContent someOtherNode.textContent = string
A string or
null
值
textContent
depends on the situation:
document
或
Doctype
,
textContent
返回
null
.
注意:
To get
all
of the text and
CDATA data
for the whole document, one could use
document.documentElement
.textContent
.
textContent
returns the text inside the node, i.e., the
Node.nodeValue
.
textContent
returns the concatenation of the
textContent
of every child node, excluding comments and processing instructions. (This is an empty string if the node has no children.)
设置
textContent
on a node removes
all
of the node's children and replaces them with a single text node with the given string value.
Don't get confused by the differences between
Node.textContent
and
HTMLElement.innerText
. Although the names seem similar, there are important differences:
textContent
gets the content of
all
elements, including
<script>
and
<style>
elements. In contrast,
innerText
only shows “human-readable” elements.
textContent
returns every element in the node. In contrast,
innerText
is aware of styling and won’t return the text of “hidden” elements.
innerText
takes CSS styles into account, reading the value of
innerText
triggers a
reflow
to ensure up-to-date computed styles. (Reflows can be computationally expensive, and thus should be avoided when possible.)
textContent
, altering
innerText
in Internet Explorer (version 11 and below) removes child nodes from the element and
permanently destroys
all descendant text nodes. It is impossible to insert the nodes again into any other element or into the same element after doing so.
Element.innerHTML
returns HTML, as its name indicates. Sometimes people use
innerHTML
to retrieve or write text inside an element, but
textContent
has better performance because its value is not parsed as HTML.
Moreover, using
textContent
can prevent
XSS attacks
.
Given this HTML fragment:
<div id="divA">This is <span>some</span> text!</div>
... you can use
textContent
to get the element's text content:
let text = document.getElementById('divA').textContent;
// The text variable is now: 'This is some text!'
... or set the element's text content:
document.getElementById('divA').textContent = 'This text is different!';
// The HTML for divA is now:
// <div id="divA">This text is different!</div>
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'Node.textContent' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
textContent
|
Chrome 1 | Edge 12 | Firefox 1 | IE 9 | Opera 9 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
完整支持
HTMLElement.innerText
Element.innerHTML
innerText
and
textContent
(blog post)
节点
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument