How to get page elements through id attribute in javascript
Jun 09, 2021 pm 04:44 PMIn JavaScript, you can use the getElementById method to obtain page elements. The syntax format is "document.getElementById("id name")". The getElementById method returns a reference to the first object with the specified ID.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Web pages organize information by tags, and the id attribute value of the tag is unique. How to find the element to be obtained through javascript
1 |
|
Through the following code, it is found that the running result is null or [object HTMLParagraphElement]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The obtained element is an object. If we want to operate on the element, we have to use its properties or methods. (object HTMLParagraphElement is the object HTML paragraph element. What is obtained is the element. This element is also the object. The obtained object will not display the value inside, so the object HTMLParagraphElement is displayed. If you want to extract the value inside, use innerHTML to obtain)
The getElementById() method returns a reference to the first object with the specified ID.
Defines multiple methods to find elements, in addition to getElementById(), there are also getElementsByName() and getElementsByTagName().
However, if you need to find a specific element in the document, the most efficient method is getElementById().
When manipulating a specific element of the document, it is best to give the element an id attribute, giving it a unique name (in the document), and then you can use that ID to find the desired element.
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to get page elements through id attribute in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot tools Tags

Hot Article

Hot tools Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to get HTTP status code in JavaScript the easy way

How to use insertBefore in javascript
