JavaScript document.querySelector() Method: Select Elements with CSS Selectors
The `document.querySelector` method is a built-in method in JavaScript that allows you to select the first element that matches a specified CSS selector within the document. It returns the first element found that matches the selector, or `null` if no element is found.
Here is an example usage of `document.querySelector`:
// Select the first element with the class \"my-element\"
const element = document.querySelector('.my-element');
// Manipulate the selected element
element.style.backgroundColor = 'red';
element.textContent = 'Hello, world!';
In this example, `document.querySelector('.my-element')` selects the first element with the class "my-element" and assigns it to the `element` variable. We can then manipulate the selected element by changing its background color and text content.
原文地址: https://www.cveoy.top/t/topic/qqgY 著作权归作者所有。请勿转载和采集!