Home > Web Front-end > JS Tutorial > How javascript changes HTML content

How javascript changes HTML content

王林
Release: 2021-10-26 16:05:23
Original
2863 people have browsed it

How to change HTML content in JavaScript: Use the innerHTML attribute to modify it, such as [document.getElementById(id).innerHTML = new text].

How javascript changes HTML content

The operating environment of this article: windows10 system, javascript 1.8.5&&html 5, thinkpad t480 computer.

The easiest way for JavaScript to modify html content is to use the innerHTML attribute. Let’s take a look at the syntax:

document.getElementById(id).innerHTML = new text
Copy after login

For example, if we want to modify the content of the

element:

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

Explanation: The above HTML document contains the

element with id="p1". We use HTML DOM to get the element with id="p1". JavaScript changes the content of the element (innerHTML) to "Hello Kitty! "

Recommended learning: javascript video tutorial

The above is the detailed content of How javascript changes HTML content. 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