Use querySelector and querySelectorAll to access DOM elements. They accept any CSS selector, so you are no longer restricted by the selection of elementsid
- Introduction
- Selector API
- Basic example from jQuery to DOM API
- A more advanced example of jQuery to DOM API
Introduction
jQuery etc.DOMIn the past, libraries and the other features they provided have greatly increased their popularity, thanks to an easy way to select page elements.
Traditionally, browsers only provide a way to select DOM elements-through itsid
Attributes withgetElementById()
,bydocument
purpose.
Selector API
Since 2013Selector API, DOM allows you to use two more useful methods:
document.querySelector()
document.querySelectorAll()
They can be used safely,As caniuse.com tells us, And apart from all other modern browsers, they are fully supported even on IE9, so there is no reason to avoid using them unless you need to support IE8 (with partial support) and below.
They accept any CSS selector, so you are no longer restricted by the selection of elementsid
.
document.querySelector()
Return a single element, the first one founddocument.querySelectorAll()
Return all the elements wrapped in the NodeList object.
These are all valid selectors:
document.querySelector('#test')
document.querySelector('.my-class')
document.querySelector('#test .my-class')
document.querySelector('a:hover')
Basic example from jQuery to DOM API
The following is a translation of converting the popular jQuery API into a native DOM API call.
Selection basisid
$('#test')
document.querySelector('#test')
we usequerySelector
sinceid
Unique in the page
Selection basisclass
$('.test')
document.querySelectorAll('.test')
Select by label name
$('div')
document.querySelectorAll('div')
A more advanced example of jQuery to DOM API
Select multiple items
$('div, span')
document.querySelectorAll('div, span')
Select by HTML attribute value
$('[data-example="test"]')
document.querySelectorAll('[data-example="test"]')
Selection via CSS pseudo-classes
$(':nth-child(4n)')
document.querySelectorAll(':nth-child(4n)')
Select the descendants of the element
Such as allli
Elements under#test
:
$('#test li')
document.querySelectorAll('#test li')
Download mine for freeJavaScript beginner's manual
More browser tutorials:
- HTML5 provides some useful tips
- How do I make a CMS-based website work offline
- The complete guide to progressive web applications
- Extract API
- Push API guide
- Channel Messaging API
- Service staff tutorial
- Cache API guide
- Notification API guide
- Dive into IndexedDB
- Selectors API: querySelector and querySelectorAll
- Load JavaScript efficiently with delay and asynchrony
- Document Object Model (DOM)
- Web storage API: local storage and session storage
- Understand how HTTP cookies work
- History API
- WebP image format
- XMLHttpRequest (XHR)
- In-depth SVG tutorial
- What is the data URL
- Roadmap for learning network platforms
- CORS, cross-domain resource sharing
- Network worker
- requestAnimationFrame() guide
- What is Doctype
- Use DevTools console and console API
- Speech Synthesis API
- How to wait for DOM ready event in pure JavaScript
- How to add classes to DOM elements
- How to traverse DOM elements from querySelectorAll
- How to remove classes from DOM elements
- How to check if a DOM element has a class
- How to change the DOM node value
- How to add the click event to the list of DOM elements returned from querySelectorAll
- WebRTC, real-time Web API
- How to get the scroll position of an element in JavaScript
- How to replace DOM elements
- How to only accept images in the input file field
- Why use the preview version of the browser?
- Blob object
- File object
- FileReader object
- FileList object
- ArrayBuffer
- ArrayBufferView
- URL object
- Type array
- DataView object
- BroadcastChannel API
- Streams API
- FormData object
- Navigator object
- How to use the geolocation API
- How to use getUserMedia()
- How to use the drag and drop API
- How to scroll on a web page
- Processing the form in JavaScript
- Keyboard events
- Mouse event
- Touch event
- How to remove all children from DOM element
- How to create HTML attributes using raw Javascript
- How to check if the checkbox is checked using JavaScript?
- How to copy to clipboard using JavaScript
- How to disable buttons using JavaScript
- How to make a page editable in the browser
- How to use URLSearchParams to get query string value in JavaScript
- How to delete all CSS on the page at once
- How to use insertAdjacentHTML
- Safari, warning before exit
- How to add an image to the DOM using JavaScript
- How to reset the table
- How to use Google fonts