Home Web Front-end JS Tutorial Detailed explanation of common selectors in jQuery

Detailed explanation of common selectors in jQuery

Jan 01, 2018 am 10:25 AM
jquery Detailed explanation Selector

This article mainly introduces the relevant information of jQuery commonly used selectors in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

This article shares the specific code of jQuery commonly used selectors for your reference. The specific content is as follows

1. jQuery: (must be marked when using jQuery The version number we use)

It is a class library that uses native JS to encapsulate commonly used methods (solve browser compatibility issues)

2. jQuery The provided method

selector

obtains the specified element/element collection in the page by passing the content of the corresponding rule (ID, tag name, style class name...)


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <p id=&#39;p1&#39;>
    <p>
      <span></span>
      <span></span>
      <span></span>
    </p>
    <p></p>
    <p id=&#39;p3&#39;></p>
    <ul>
      <li></li>
      <li></li>
      <li></li>
    </ul>

  </p>
  

  <script>
    //原生JS获取到的结果属于元素对象/元素集合/节点集合...他们可以使用浏览器为其提供的那些天生自带的属性和方法
    //原生的JS对象不能直接的使用jQuery中提供的属性和方法
    var op = document.getElementById(&#39;p1&#39;)
    op.clientWidth
    op.getAttribute
    //jq获取到的结果是一个jQuery对象,可以使用jQuery里面提供的属性和方法,但是不能直接的使用浏览器内置的属性和方法
    var $op = jQuery("#p1")//$("#p1")
    $op.innerWidth();
    $op.attr

    //关于原生JS对象和jQuery对象之间的转换
      //1)、原生的转变成jQuery:$(原生JS对象)
      $(op)
      //2)、jQuery转化成原生:直接通过索引获取对应的元素对象即可
      $op[0]
      $op.get(0)//<==>$op[0]都是通过索引来获取指定位置的元素(JS原生对象)

    //更多的jQuery选择器
    $(&#39;#p1&#39;)
    $(&#39;p&#39;)
    $(&#39;.w100&#39;)
    $(&#39;*&#39;)
    $(&#39;#p1,p,.w100&#39;)//把每一个选择器获取到的jQuery对象最后融合在一起,最后一起获取到
    $(&#39;#p1 li&#39;)//在子子孙孙级中进行查找
    $(&#39;#p1>li&#39;)//在子级中进行查找
    $(&#39;#p3+&#39;)//获取它的下一个弟弟
    $(&#39;#p3+ul&#39;)//获取它的下一个弟弟并且标签名是ul的
    $(&#39;#p3~&#39;)//获取它的所有的弟弟元素
    $(&#39;#p3~ul&#39;)//获取它的所有的弟弟元素并且标签名为ul的
    $(&#39;#p1>p:not(.w100)&#39;)//#p1下的所有子集p样式类名不包含w100的
    $(&#39;#p1>p:eq(0)&#39;)//通过索引获取到集合中的某一个,但是获取到的结果依然是一个jQuery对象(而get方法也是通过索引获取,但是获取到的是一个JS原生对象)
    $(&#39;#p1>p:gt(1)&#39;)//大于索引1的(不包含索引1的)
    $(&#39;#p1>p:lt(1)&#39;)//小于索引1的(不包含索引1的)
    $(&#39;#p1 li:contains("我")&#39;)//获取所有的li内容包含“我” 的
    $(&#39;#p1 p:has(ul)&#39;)//在所有的p中包含ul的
    $(&#39;#p1>*:nth-child(1)&#39;)//获取所有的子元素的第一个
    $(&#39;#p1>*:eq(1)&#39;)//获取所有的子元素的第二个(索引为1)
  </script>
</body>
</html>
Copy after login

Related recommendations:

Detailed explanation of how to handle special symbols in jQuery selectors

jQuery UI date picker Datepicker detailed explanation

Example detailed explanation jQuery form object attribute filter selector

The above is the detailed content of Detailed explanation of common selectors in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

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

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

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: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

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:

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

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

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

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

See all articles