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

How to change text content in javascript

青灯夜游
青灯夜游Original
2021-04-07 18:51:596709browse

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>

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!

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