Get the inner text of an element:
let text = element.innerText;
Try it Yourself »
More examples below.
The innerText
property sets or returns the text content of an element.
When you set the innerText property, all child nodes are removed and replaced by only one new text node.
See below
Return the text content of an element or node:
element.innerText
node.innerText
Set the text content of an element or node:
element.innerText =
text
node.innerText =
text
Property | Description |
text | The text content of the element. |
Type | Description |
String | The text content of the element and all descendants, except for <script> and <style> elements. |
The innerHTML property returns: The text content of the element, including all spacing and inner HTML tags. |
The innerText property returns: Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements. |
The textContent property returns: The text content of the element and all descendaces, with spacing and CSS hidden text, but without tags. |
<p id="myP"> This element has extra spacing and contains <span>a span element</span>.</p>
let text = document.getElementById("myP").innerText;
let text = document.getElementById("myP").innerHTML;
let text = document.getElementById("demo").textContent;
Try it Yourself »
In the example above:
The innerText property returns: This element has extra spacing and contains a span element. |
The innerHTML property returns: This element has extra spacing and contains <span>a span element</span>. |
The textContent property returns: This element has extra spacing and contains a span element. |
element.innerText
is supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 10-11 | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!