Latest web development tutorials

HTML DOM textContent property

Elements Object Reference Element object

Examples

Get the text content of the first button element:

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

Output:

尝试一下

try it"

Definition and Usage

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 with a text node is specified string.

Tip: In some cases textContent property nodeValue property can be substituted, but remember that this property can also return all text child nodes.

Examples

Get all the text content of the list:

document.getElementById("myList).textContent;

try it"

Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support textContent property

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


grammar

Set the node text:

node .textContent= text

Returns the node text:

node .textContent

technical details

return value: String, text node and all nodes
DOM version Core Level 3 Node Object


Elements Object Reference Element object