Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use innerHTML in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 10:40:15 09月23日 Update

Most people do not understand the knowledge points of this article "how to use innerHTML in JavaScript", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use innerHTML in JavaScript" article.

How to use JavaScript innerHTML

(1) innerHTML can insert HTML code directly into specified nodes.

Syntax:

NodeObject [XSS _ clean] = HTMLCode

Where nodeObject is the node object and HTMLCode is the HTML code.

Note: innerHTML will overwrite the original HTML code.

For example, add three new nodes to the node of id= "demo":

Div id= "demo" >

Div > Click here to add a new node / div >

/ div >

Script type= "text/javascript" >

Document.getElementById ("demo") .onclick=function () {

This [XSS _ clean] = "div > this is the new node 1/div > div > this is the new node 2/div > div > this is the new node 3/div >"

}

/ script >

How does Javascript get sibling nodes

The so-called sibling node refers to the previous node or the next node of the current node.

Get the previous node

In Javascript, the previous node is obtained through previousSibling.

Syntax:

NodeObject.previousSibling

Where nodeObject is the node object (element node).

Under IE, blank nodes between nodes (spaces, carriage returns, and Tab keys) are ignored; not under browsers that follow the W3C specification (Chrome, FireFox, Safari, etc.).

Take a look at the following code:

# demo {

Height:180px

Width:250px

Padding:0px 10px 10px 10px

Border:1px solid # ccc

Background-color:#ededed

}

# demo div {

Height:50px

Width:250px

Margin-top:10px

Text-align:center

Line-height:50px

Background-color:#ccc

}

The above is about the content of this article on "how to use innerHTML in JavaScript". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Nodes content code this is brothers objects articles knowledge articles grammar three values elements most that is more steps browsers knowledge points spaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Xiaomi Linux vpn