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 change the content and attribute of HTML element in JavaScript HTML DOM

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to change the content and attributes of HTML elements in JavaScript HTML DOM. I hope you will get something after reading this article. Let's discuss it together.

JavaScript HTML DOM-change HTML

HTML DOM allows JavaScript to change the contents of the HTML element.

Change the HTML output stream

JavaScript can create dynamic HTML content, such as getting the current date and time.

In JavaScript, [xss_clean] () can be used to output stream writes directly to HTML.

[xss_clean] (Date ())

Tip: never use [xss_clean] () after the document is loaded. This overwrites the document.

Change HTML content

The easiest way to modify HTML content is to use the innerHTML property.

To change the contents of the HTML element, use this syntax:

Document.getElementById (id) [xss_clean] = new HTML

The following example has changed

Contents of the element:

Hello World!

Document.getElementById ("p1") [xss_clean] = "New text!"

The above paragraph has been modified by a JavaScript script.

The following example changes the content of the element:

Old Header var element=document.getElementById ("header"); Element [XSS _ clean] = "New Header"

"Old Header" has been modified to "New Header".

Note: use HTML DOM to get the element of id= "header", and JS changes the content of this element (innerHTML).

Change the HTML property

To change the attributes of the HTML element, use this syntax:

Document.getElementById (id). Attribute=new value

The following example has changed

The src attribute of the element:

Document.getElementById ("image") .src = "landscape.jpg"

The original picture is smiley.gif, but it has been modified to landscape.jpg.

Note: use HTML DOM to get the element of id= "image", and JavaScript changes the attributes of this element.

After reading this article, I believe you have a certain understanding of "how to change the content and attributes of HTML elements in JavaScript HTML DOM". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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: 0

*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

Development

Wechat

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

12
Report