English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Objects

JavaScript Functions

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JS HTML DOM Selectors

JavaScript is most commonly used to get or modify the content or value of HTML elements and apply certain effects.

To do this, you must first find the element. There are many ways to do this:

  • to find HTML elements by ID

  • to find HTML elements by tag name

  • Find HTML elements through class name

  • Find HTML elements through CSS selectors

  • Find HTML elements through HTML object collections

to find HTML elements by ID

You can usegetElementById()The method selects elements based on the unique ID of the element.

This is the simplest way to find HTML elements in the DOM tree.

The following example selects an element with an ID attributeid="msg"elements:

var x = document.getElementById("msg");
Test and see‹/›

If the element is found, this method will return the element as an object.

If the element is not found, myElement will contain null.

to find HTML elements by tag name

You can also usegetElementsByTagName()The method selects HTML elements by tag name.

This method returns a list similar to an array of all elements in the document with the specified tag name.

This example selects all<p>Element:

var x = document.getElementsByTagName("p");
Test and see‹/›

Change the background color of all paragraph elements in the document:

var x = document.getElementsByTagName("p");
for(let i = 0; i < x.length;++) {
   x[i].style.backgroundColor = "coral";
}
Test and see‹/›

This example selectsid="wrapper"elements, then select“wrapper”all elements inside<p>Element:

var x = document.getElementById("wrapper");
var y = x.getElementsByTagName("p");
Test and see‹/›

Find HTML elements through class name

You can use thegetElementsByClassName()Method to select all elements with a specific class name.

This method returns a list similar to an array of all elements in the document with the specified class name.

This example returns all elements withclass="demo"List of elements:

var x = document.getElementsByClassName("demo");
Test and see‹/›

Find HTML elements through CSS selectors

You can use thequerySelectorAll()Method to select elements that match the specified CSS selector (ID, class, type, etc.).

This method returns a list similar to an array of all elements that match the specified selector.

CSS selectors provide a very powerful and effective method for selecting HTML elements in the document.

var x = document.querySelectorAll("div");
Test and see‹/›

This example returns elements with"note"All<div>List of elements:

var x = document.querySelectorAll("div.note");
Test and see‹/›

Find HTML elements through HTML object collections

The top-level element of an HTML document can be used directly as a document property.

For example, you can use the property to access<html>Elementdocument.documentElement.

TheComponents can be accesseddocument.bodyProperty.

var html = document.documentElement;
var body = document.body;
Test and see‹/›

Note:Ifdocument.bodyInUsed before a tag (for example, inInside <head>), which will return null instead of the body element.

You can also access the following HTML objects (and object collections):

PropertyDescription
document.anchorsReturns all elements with a name attribute<a>Element
document.appletsReturn all<applet>Element(In HTML5(Deprecated)
document.baseURIReturns the absolute base URI of the document
document.bodyReturnElement
document.cookieReturns the cookie of the document
document.doctypeReturns the document type of the document
document.documentElementReturn<html>Element
document.documentModeReturns the mode used by the browser
document.documentURIReturns the URI of the document
document.domainReturn the domain name of the document server
document.domConfigDeprecated;Return the DOM configuration
document.embedsReturn all<embed>Element
document.formsReturn all<form>Element
document.headReturn<head>Element
document.imagesReturn all<img>Element
document.implementationReturn the DOM implementation
document.inputEncodingReturn the encoding (character set) of the document
document.lastModifiedReturn the date and time of the document update
document.linksReturn all elements with the href attribute<area>and<a>Element
document.readyStateReturn the (loading) status of the document
document.referrerReturn the URI of the referer (link document)
document.scriptsReturn all<script>Element
document.strictErrorCheckingReturn whether strict error checking is enforced
document.titleReturn<title>Element
document.URLReturn the complete URL of the document