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

How to set element text content in javascript

青灯夜游
青灯夜游Original
2021-04-26 17:06:067140browse

Javascript method to set text content: First use the "document.getElementById("id value")" statement to obtain the text element object; then use the "text element object.innerHTML='text content value';" statement that is Can be set.

How to set element text content in javascript

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

javascript sets element text content

Steps:

  • Find element

  • Set content using innerHTML

Example:

<body>
    <p id="demo"></p>
    <script type="text/javascript">
        var x=document.getElementById("demo");  // 找到元素
        x.innerHTML="Hello JavaScript!";    // 设置内容
    </script>
    
</body>

Output:

How to set element text content in javascript

Description:

The innerHTML attribute sets or returns the HTML between the start and end tags of the table row.

Syntax

tablerowObject.innerHTML=HTML

Methods to find elements:

1. Get elements based on id

document.getElementById("id属性的值");

2. Get elements based on tag names

document.getElementsByTagName("标签的名字");

3. Get the element based on the value of the name attribute

document.getElementsByName("name属性的值");

4. Get the element based on the class attribute

document.getElementsByClassName("类样式的名字");

5. Get the element based on the css path (get one)

document.querySelector("css路径");

6. Get elements based on css path (get a group)

document.querySelectorAll("css路径");

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to set element 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