Home > Web Front-end > JS Tutorial > How to modify html using javascript

How to modify html using javascript

coldplay.xixi
Release: 2023-01-05 16:13:14
Original
3646 people have browsed it

How to modify HTML using JavaScript: 1. Use [document.write()] to directly write content to the HTML output stream; 2. Use the innerHTML attribute to change the HTML content; 3. Change the HTML attributes.

How to modify html using javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript method to modify html:

Change the HTML output stream:

In JavaScript, document.write() Can be used to write content directly to the HTML output stream

<!DOCTYPE html>
<html>
<body>
<script>
document.write(Date());
</script>
</body>
</html>
Copy after login

Do not use document.writr() after the document is loaded. This will overwrite the document.

Change HTML content

The easiest way to modify HTML content is to use the innerHTML attribute

<html>
<body>
<p id="p1">Hello World!</p>
<script>
document.getElementById("p1").innerHTML="New text!";
</script>
</body>
</html>
Copy after login

Change HTML attribute

This example changes the src attribute of the element

<!DOCTYPE html>
<html>
<body>
<img id="image" src="smiley.gif">
<script>
document.getElementById("image").src="landscape.jpg";
</script>
</body>
</html>
Copy after login

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to modify html using javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template