Detailed analysis of commonly used selectors in jQuery
This article shares the specific code of jQuery's commonly used selectors for your reference. The specific content is as follows
1. jQuery: (When using jQuery, you must indicate the version number we use)
It is a class library that uses native JS to encapsulate commonly used methods (solve browser compatibility issues)
2. Methods provided in jQuery
Selector
By passing the content of the corresponding rule (ID, tag name, style class name...), the elements/element collection specified in the page are obtained.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id='div1'> <div> <span></span> <span></span> <span></span> </div> <div></div> <div id='div3'></div> <ul> <li></li> <li></li> <li></li> </ul> </div> <script> //原生JS获取到的结果属于元素对象/元素集合/节点集合...他们可以使用浏览器为其提供的那些天生自带的属性和方法 //原生的JS对象不能直接的使用jQuery中提供的属性和方法 var oDiv = document.getElementById('div1') oDiv.clientWidth oDiv.getAttribute //jq获取到的结果是一个jQuery对象,可以使用jQuery里面提供的属性和方法,但是不能直接的使用浏览器内置的属性和方法 var $oDiv = jQuery("#div1")//$("#div1") $oDiv.innerWidth(); $oDiv.attr //关于原生JS对象和jQuery对象之间的转换 //1)、原生的转变成jQuery:$(原生JS对象) $(oDiv) //2)、jQuery转化成原生:直接通过索引获取对应的元素对象即可 $oDiv[0] $oDiv.get(0)//<==>$oDiv[0]都是通过索引来获取指定位置的元素(JS原生对象) //更多的jQuery选择器 $('#div1') $('div') $('.w100') $('*') $('#div1,div,.w100')//把每一个选择器获取到的jQuery对象最后融合在一起,最后一起获取到 $('#div1 li')//在子子孙孙级中进行查找 $('#div1>li')//在子级中进行查找 $('#div3+')//获取它的下一个弟弟 $('#div3+ul')//获取它的下一个弟弟并且标签名是ul的 $('#div3~')//获取它的所有的弟弟元素 $('#div3~ul')//获取它的所有的弟弟元素并且标签名为ul的 $('#div1>div:not(.w100)')//#div1下的所有子集div样式类名不包含w100的 $('#div1>div:eq(0)')//通过索引获取到集合中的某一个,但是获取到的结果依然是一个jQuery对象(而get方法也是通过索引获取,但是获取到的是一个JS原生对象) $('#div1>div:gt(1)')//大于索引1的(不包含索引1的) $('#div1>div:lt(1)')//小于索引1的(不包含索引1的) $('#div1 li:contains("我")')//获取所有的li内容包含“我” 的 $('#div1 div:has(ul)')//在所有的div中包含ul的 $('#div1>*:nth-child(1)')//获取所有的子元素的第一个 $('#div1>*:eq(1)')//获取所有的子元素的第二个(索引为1) </script> </body> </html>
Element selection is the prerequisite for all operations. One of the most powerful and commonly used functions of the $() function in jQuery is to use selectors to select DOM elements. Here is a summary of some very commonly used jquery selectors.
1. Basic structure of jQuery selector
$('选择器') $('选择器 上下文')
2. Using basic css selector
About basic css selection You can take a look at the detailed explanation of css selector. Here are some of the most basic ways to use css selectors.
2.1 Element Selector
$('a'); //选择所有a元素 $('div'); //选择所有div元素 $('p'); //选择所有p元素
Of course, if you want, jQuery also allows us to use commas to combine multiple selectors into one selector:
$('a,div,p');
This achieves the same effect as the above three lines of code.
2.2 Class selector
$('div.myClass'); //所有拥有myClass类的div元素 $('p.myClass'); //所有拥有myClass类的p元素 $('*.myClass'); //拥有myClass类的所有类型元素
Normally, when you want to select all elements with a certain class, the wildcard * will be omitted, as follows:
$('.myClass'); //拥有myClass类的所有类型元素
There is nothing wrong with this, and it is also our common way of writing.
In addition, some elements may have more than one class:
$('div.myClass1.myClass2');
This will select div elements that have both myClass1 and myClass2 classes. Of course, the selected div element may also have other classes, that is to say, the following div will be selected without any doubt:
<div class="myClass1 myClass2 myClass3">...</div>
2.3 ID Selector
$('table#myID'); //id为myID的table元素
3. Combined use of context selectors
Start from here and start some slightly more difficult selections, such as:
$('ul.myUl li');
This will select all li sub-elements that have ul elements of myUl class. It sounds like a mouthful. Look at the following code:
html
<ul class="myUl"> <li><a href="#"></a> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </li> <li> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> </li><ul>
Here, through $('ul.myUl li'), all li elements will be selected, note that all! Because all li elements are descendants of
- ...
In fact, the above example is not enough to fully explain the meaning of all li sub-elements of the ul element with myUl class. Because there may be more than one ul element with the myUl class, as follows:
html
<ul class="myUl"> <li><a href="#"></a> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </li> <li> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> </li><ul>
-
- 一
- 二
- 三
$('ul.myUl li') will also select all li elements in the above code. Because all li elements in the above code are child elements of ul.myUl, although there are 2 ul.myUl. Now you should be able to understand the meaning of all li sub-elements that have the ul element of the myUl class!
The descendant selector can actually not only select the descendants of a certain element, but also the descendants of the descendants of a certain element (which sounds a bit awkward), as follows:
$('ul.myUl li a');
This selects all the descendants of myUl All a descendant elements of all li descendant elements of the class's ul element. Although there is one more descendant of xx, it is the same as the above analysis, so I won’t go into details.
The above is the detailed content of Detailed analysis of commonly used selectors in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

目录1:basename()2:copy()3:dirname()4:disk_free_space()5:disk_total_space()6:file_exists()7:file_get_contents()8:file_put_contents()9:filesize()10:filetype()11:glob()12:is_dir()13:is_writable()14:mkdir()15:move_uploaded_file()16:parse_ini_file()17:
