Javascript gets DOM node
There are many ways to obtain DOM nodes. You can obtain them based on the id attribute and tag name, or you can obtain child nodes, parent nodes, previous nodes, and next nodes.
This section explains how to obtain nodes based on the id attribute and label name of HTML tags.
getElementById() method
To obtain DOM nodes based on the id attribute of the HTML tag, please use the getElementById() method. This method returns a node object.
Syntax:
document.getElementById(id)
Among them, id is the id attribute of the HTML tag.
For example, the statement to obtain the node with id="demo" is:
The return value of this statement is [object HTMLDivElement] (element node object).
For example, get several typical element nodes:
HTML DOM树型结构图 我是标签我是
标签
我是
标签
Please see the following demonstration
getElementsByTagName( ) method
To obtain DOM nodes based on HTML tag names, please use the getElementsByTagName( ) method . This method returns the obtained element node as an array.
Syntax:
nodeObject.getElementsByTagName(tagName)
Among them, nodeObject is the element node and tagName is the name of the HTML tag.
Note: The getElementsByTagName() method can not only search all nodes in the entire HTML document, but also search the child nodes of a certain node. When using it, you must specify the search range, that is, specify nodeObject.
For example, to get all thetags in the HTML document:document.getElementsByTagName("div");
Get all the < inside the tag with id="demo" ;div> tag:
document.getElementById("demo").getElementsByTagName("div");
For example, count the number of all
tags and display their Text:我是第 1 个标签我是第 2 个标签我是第 3 个标签我是第 4 个Continuing Learning标签我是第 5 个标签我是第 6 个标签Note:
getElementById() is a method of document (root node), other element nodes cannot use this method. Because the id attribute is unique in the entire HTML document, it must be searched starting from the root node.
getElementsByTagName() is a method of all element nodes, not only the document (root node) can be used, but other nodes can also be used. Therefore, to obtain DOM nodes based on HTML tag names, you can not only search from the document (root node), but also from any other element node.
||HTML DOM树型结构图 我是第 1 个标签我是第 2 个标签我是第 3 个标签我是第 4 个
- Course Recommendations
- Courseware download
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watchingElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watchingIntermediateIssue 22_Comprehensive actual combat
5521 people are watchingElementaryIssue 22_PHP Programming
5172 people are watchingElementaryIssue 22_Front-end development
8713 people are watchingIntermediateBig data (MySQL) video tutorial full version
4525 people are watchingElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watchingElementaryGO Language Core Programming Course
2814 people are watchingIntermediateJS advanced and BootStrap learning
2563 people are watchingIntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watchingIntermediateRedis+MySQL database interview tutorial
2963 people are watchingElementaryDeliver food or learn programming?
5708 people are watchingThe courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!