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

How to modify html using javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-30 11:59:133644browse

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>

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>

Change HTML attribute

This example changes the src attribute of the a1f02c36ba31691bcfe87b2722de723b element

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

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!

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