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).
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.
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."
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.
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.
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!