The getElementById() method is a JavaScript method used to retrieve a specific HTML element based on its unique ID. It's commonly used to manipulate or modify the content, attributes, or styles of an element dynamically using JavaScript code.

Syntax: document.getElementById('id');

Example:

HTML code:

This is my div element

JavaScript code: var myDiv = document.getElementById('myDiv'); myDiv.style.color = 'red'; // changes the text color to red

In this example, the getElementById() method is used to retrieve the div element with the ID 'myDiv' and assign it to the variable 'myDiv'. Then, the style.color property of the div element is modified to change the text color to red.

JavaScript getElementById() Method: Get HTML Elements by ID

原文地址: https://www.cveoy.top/t/topic/lARz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录