Home  >  Article  >  Web Front-end  >  How javascript changes HTML content

How javascript changes HTML content

王林
王林Original
2021-10-26 16:05:232821browse

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

For example, if we want to modify the content of the e388a4556c0f65e1904146cc1a846bee element:

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

Explanation: The above HTML document contains the e388a4556c0f65e1904146cc1a846bee 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!

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