Current location - Quotes Website - Personality signature - Usage and difference between innerHTML and innerText in js
Usage and difference between innerHTML and innerText in js
The usage of innerHTML in 1 and js:

Innerhtml can get or set the html content in the specified element tag, from the start position to the end position of the element tag, including the html tag.

Get the content of the element: element.innerHTML

Set the content of the element: element.innerHTML = htmlString.

The code example is:

& LTP id = "test" >< fontcolor = "# 000" > get innerhtml.

document.getElementById("test ")。 innerHTML

The output content is:

2. the usage of innerText in 2.js:

InnerText can get or set the text value in the specified element tag, that is, all the text content from the start position to the end position of the element tag (excluding the html tag).

Get the content of the element: element.innerText

Set the content of the element: element.innerText = string.

The code example is:

& LTP id = "test" >< fontcolor = "# 000" > get innerhtml.

document.getElementById("test ")。 innerHTML

The output is: get the innerHTML test of paragraph p.

3. the difference between 3.innerHTML and innerText:

Innerhtml returns the html content inside the tag, including the html tag.

InnerText returns the text value inside the tag, without the html tag.

The code example is:

& LTP id = "test" >< font color = "# 000" > get paragraph p.

document.getElementById("test ")。 innerHTML

The output content is:

document.getElementById("test ")。 Internal text

The output content is: get paragraph p test.

Extended data:

1, get javascript of node text value:

(1) Native js is written as document. getElementByid ('test ')。 innerHTML。

(2)jQuery is written as $('#test'). html()

2. The method of getting nodes by 2.javascript:

(1) by id

document.getElementById("id ")?

(2) Find elements by class name, and separate multiple class names with spaces to get an HTMLCollection (a collection of elements with length attribute, one of which can be accessed by index number).

document . getelementsbyclassname(' a b ')

(3) Search for elements by tag names and return to HTMLCollection.

document . getelementsbytagname(' div ')

(4) Search by the name attribute, and return a NodeList (the node set by the length attribute can be accessed by the index number).

document.getElementsByName('c ')

Official Javascript document -Element.innerHTML

Official Javascript document -HTMLElement.innerText