How to use innerHTML in JavaScript
This article mainly explains "how to use innerHTML in JavaScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use innerHTML in JavaScript.
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
}
Thank you for your reading, the above is the content of "how to use innerHTML in JavaScript". After the study of this article, I believe you have a deeper understanding of how to use innerHTML in JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!