Home > Article > Web Front-end > What are the methods for positioning elements in JavaScript?
Methods to locate elements: 1. Use getElementById("id"); 2. Use "name"; 3. Use "element tag name"; 4. Use "class"; 5. Use "css selection" device".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
What are the methods for locating elements in JavaScript
Except for the id, which locates a single element element object, everything else is returned by elements. It is a list object
1. Get by id
document.getElementById(“id”)
2. Get by name
document.getElementsByName(“Name”)
returns list
3 .Select elements by tag name
document.getElementsByTagName(“tag”)
4.Select elements by CLASS class
document.getElementsByClassName("class")
Compatibility: IE8 and below browsers do not implement the getElementsByClassName method
5. Select elements through CSS selectors
document.querySelectorAll("css selector")
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What are the methods for positioning elements in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!