function $(tag) { let tage = document.querySelectorAll(tag); return tage.length == 1 ? tage[0] : tage; }
时间: 2024-05-29 12:09:41 浏览: 9
This is a JavaScript function that takes a string argument "tag" which represents a tag name (e.g. "div", "p", "ul", etc.).
The function then uses the document.querySelectorAll() method to find all elements in the document that match the specified tag name. This method returns a NodeList, which is an array-like object that contains all the matching elements.
The function then checks if the length of the NodeList is equal to 1. If it is, it returns the first (and only) element in the NodeList. If not, it returns the entire NodeList.
This function essentially allows you to select one or multiple elements in the document using their tag name, and returns them in a convenient and easy-to-use format. It is commonly used in JavaScript programming to manipulate the DOM (Document Object Model) and create dynamic web pages.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)