HTML DOM textContentProperty

元素对象参考手册Element Object

Examples

Get the text content of the first button element:

document.getElementsByTagName("BUTTON")[0].textContent;

Output result:

Try it yourself

Try it Yourself »

Definition and Usage

The textContent property sets or returns the text content of the specified node.

If you set the textContent property, any child nodes are removed and replaced by a single text node containing the specified string.

Tip:Sometimes the textContent property can be replaced by the nodeValue property, but remember that this property can also return the text of all child nodes.

Examples

Get the text content of all lists:

document.getElementById("myList).textContent;

Try it Yourself »

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the textContent property.

Note:Internet Explorer 8 and earlier IE versions do not support this property.


Syntax

Set the text content of a node:

node.textContent=text

Return the text content of a node:

node.textContent

Technical Details

Return value: String, representing the text of the node and all its descendant nodes.
DOM Version Core Level 3 Node Object


元素对象参考手册Element Object

Other Extensions