Home > Web Front-end > JS Tutorial > How to change text content in javascript

How to change text content in javascript

青灯夜游
Release: 2023-01-04 09:34:56
Original
6742 people have browsed it

Javascript method to change text content: First use the "document.getElementById("element id value")" statement to obtain the element object; then use the innerHTML attribute to change the text content, the syntax format is "element object.innerHTML= text;".

How to change text content in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript changes the text content of the html web page

<body>
    <p id="demo">JavaScript能改变html文本内容</p>
    <script type="text/javascript">
        function myFunction() {
            x=document.getElementById("demo");  // 找到元素
            x.innerHTML="Hello JavaScript!";    // 改变内容
        }
    </script>
    <button type="button" οnclick="myFunction()">点击这里</button>
</body>
Copy after login

Code screenshot:

How to change text content in javascript

Demo screenshot:

How to change text content in javascript

How to change text content in javascript

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to change text content in 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