Home  >  Article  >  Web Front-end  >  Summarize the methods of obtaining DOM elements in JavaScript

Summarize the methods of obtaining DOM elements in JavaScript

yulia
yuliaOriginal
2018-10-08 11:43:553054browse

In work, we often need to obtain certain elements, and then modify the styles, contents, etc. of these elements. Do you know which methods can be used to obtain DOM elements? This article will tell you about the various methods of obtaining and finding elements in DOM in JavaScript, including getElementById(), getElementsByName(), etc. Friends who need it can refer to it. I hope it will be helpful to you.

1. getElementById()

Get the specified element through the specified id attribute value. If there is no element with the specified ID, null is returned; if there are multiple elements with the specified ID, undefined is returned. All browsers support the getElementById() method.

Writing method: document.getElementById(id)

Example: When the h1 tag is clicked, a prompt box will pop up to display the value in the h1 tag. The code is as follows:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
 </head>
 <body>
  <h1 id="title" onclick="myValue()">我是标题h1</h1>
 </body>
 <script type="text/javascript">
  function myValue()
   {
   var a=document.getElementById("title")
   alert(a.innerHTML)
   }
 </script>
</html>

Rendering:

Summarize the methods of obtaining DOM elements in JavaScript

2. getElementsByClassName()

Get elements through the specified class attribute value . It returns a collection of elements with the specified class name, which is a collection of objects. getElementsByClassName can be called on any element, not just document. The element calling this method will be used as the root element of this search.

Browser support is as follows:

Summarize the methods of obtaining DOM elements in JavaScriptWriting method: document.getElementsByClassName(names)

##3. getElementsByTagName

Get elements through a specific tag name, which returns a collection with the specified tag name, which is a collection of objects. The getElementsByTagName() method returns elements in the order they appear in the document. All browsers support the getElementsByTagName() method.

Writing method: document.getElementsByTagName(tagname)

4. getElementsByName(name)

The getElementsByName method is similar to the getElementById method, the difference is the former Query the name attribute of the element, which queries the id attribute of the element. It returns a collection of objects with the specified name. All browsers support the getElementsByName() method.

Writing method: document.getElementsByName(name)

The above introduces the four methods of obtaining elements in JavaScript, including getElementById(id), getElementsByClassName(names), getElementsByTagName(tagname), getElementsByName( name), which method to use depends on your work needs. I hope this article will be helpful to you!

The above is the detailed content of Summarize the methods of obtaining DOM elements in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn