English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
getElementById()The method can obtain an element node object based on the element's id attribute, and this method is available in all browsers.
Since if an element ID is specified, it must be unique, making them an effective method for quick access to specific elements.
If you need to access an element without an ID, you can usequerySelector()Through anySelectorFind the element.
document.getElementById(ID)
document.getElementById('para');Test See‹/›
All browsers fully support the getElementById() method:
Method | |||||
getElementById() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
ID | The ID of the element to be located. ID is a case-sensitive string that is unique in the document. Only one element can have any given ID. |
Return Value: | Description of the DOM element object that matches the specified IDElementobject; if the matching element is not found in the document, it isnull. |
---|---|
DOM Version: | DOM 2Level |
Get the element with id="para" and change its color:
document.getElementById('para').style.color = "blue";Test See‹/›
Get the element with id="para" and change its font size:
document.getElementById('para').style.fontSize = ""3em";Test See‹/›
CSS Tutorial:CSS Syntax
CSS Reference:CSS #idSelector
DOM Document querySelector()Methods
of DOM DocumentgetElementsByClassName()Methods
of DOM DocumentgetElementsByTagName()Methods