Home  >  Article  >  Web Front-end  >  Several common methods to control html elements in JavaScript

Several common methods to control html elements in JavaScript

PHPz
PHPzOriginal
2023-04-13 10:46:09843browse

HTML Methods of JS

JavaScript is a scripting language that can be used to control the content of HTML documents and web pages. In JavaScript, you can use HTML methods to manipulate elements in HTML documents. HTML methods are provided through the JavaScript DOM (Document Object Model).

  1. getElementById method

The getElementById method is one of the most commonly used HTML methods in JavaScript. It allows you to get elements in an HTML page based on their ID. The following is a simple example:

<!DOCTYPE html>
<html>

<head>
    <title>getElementById例子</title>
</head>

<body>
    <h1 id="myHeader">这是一个标题</h1>
    <script>
        var header = document.getElementById("myHeader");
        header.style.color = "red";
    </script>
</body>

</html>

In this example, we use the getElementById method to obtain an element with the ID "myHeader" and set its color to red.

  1. innerHTML method

The innerHTML method can be used to get or set the HTML content of a certain element. The following is an example:

<!DOCTYPE html>
<html>

<head>
    <title>innerHTML例子</title>
</head>

<body>
    <div id="myDiv">这是一个div元素。</div>
    <script>
        var div = document.getElementById("myDiv");
        div.innerHTML = "这是一个新的内容。";
    </script>
</body>

</html>

In this example, we use the innerHTML method to set the content of a div element to "This is a new content."

  1. style method

The style method allows you to change the style of an HTML element. For example, you can use the style attribute to change the element's color, background color, size, etc. Here is an example:

<!DOCTYPE html>
<html>

<head>
    <title>改变文字颜色</title>
</head>

<body>
    <h1 id="myHeader">这是一个标题</h1>
    <button onclick="changeColor()">更改颜色</button>
    <script>
        function changeColor() {
            var header = document.getElementById("myHeader");
            header.style.color = "red";
        }
    </script>
</body>

</html>

In this example, we have created a button, and when the button is clicked, the changeColor function will be called to change the title color to red.

  1. createElement method

The createElement method can be used to create new HTML elements. Here is an example:

<!DOCTYPE html>
<html>

<head>
    <title>createElement例子</title>
</head>

<body>
    <button onclick="createDiv()">创建新的div元素</button>
    <script>
        function createDiv() {
            var newDiv = document.createElement("div");
            newDiv.innerHTML = "这是一个新的div元素";
            document.body.appendChild(newDiv);
        }
    </script>
</body>

</html>

In this example, we create a button, when the button is clicked, the createDiv function will be called, which will create a new div element and add it to the HTML page middle.

  1. getElementsByTagName method

The getElementsByTagName method can get the elements in the HTML page through the tag name. Here is an example:

<!DOCTYPE html>
<html>

<head>
    <title>getElementsByTagName例子</title>
</head>

<body>
    <h1>这是一个标题</h1>
    <p>这是一个段落。</p>
    <p>这是另一个段落。</p>
    <script>
        var paragraphs = document.getElementsByTagName("p");
        for (var i = 0; i < paragraphs.length; i++) {
            paragraphs[i].style.color = "red";
        }
    </script>
</body>

</html>

In this example, we use the getElementsByTagName method to get all the paragraph elements and set their color to red.

Summary

In JavaScript, you can use HTML methods to control elements in an HTML document. Commonly used HTML methods include getElementById, innerHTML, style, createElement and getElementsByTagName. These methods give us greater control over the content and style of HTML pages.

The above is the detailed content of Several common methods to control html elements 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