Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use innerHTML in JavaScript

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 272

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report