search
HomeWeb Front-endJS TutorialOrganized collection based on jQuery selector_jquery

jquery object access
1. each (callback): execute a function with each matching element as the context, return false; stop the loop; return true; jump to the next loop.

Let’s give an example: 

Copy the code The code is as follows:

$("img").each(function(){
$(this).toggle("example");
})

2. size() and length The same, both return the number of elements in the jquery object.

$("img").size(); or $("img").length;

3. get(): Get the set of all matching DOM elements (note that the return is a dom object, not a jquery object)

Copy code The code is as follows:

$("img").get().reverse();

4. get(index): get one of them matching elements. Index indicates which element is matched. Using the get(index) method allows you to operate an actual DOM element.

$("img").get(0);//Get the first matching img element

$(this).get(0) is equivalent to $(this)[0]

5. index(subject): Search for elements that match the object represented by the parameter, and return the index value of the corresponding element.

Selector-Basic
selector1, selector2, selectorN will merge the matched elements and return them together

Copy code The code is as follows:

$("div,span,p.myClass");

There are several similar syntax differences to note here:

1.$("span",this)

For example:

Copy code The code is as follows:

$("div.foo" ).click(function() {
$("span", this).addClass("bar");
});

2. $("div#hi "), $("p.intro")

For example:

Copy code The code is as follows:

$("div#hi" ).css("color","red");

3. $("form input")

For example:

Copy code The code is as follows:

$("form input") .css("border", "5px solid red");

要区别上面几种相似形式的不同意思。

选择器-层级
1.ancestor descendant在给定的祖先元素下匹配所有的后代元素

$("div input");//div下所有input

2.parent > child 在给定的父元素下匹配所有的子元素

$("div > input);//父元素下的子元素

3.prev + next 匹配所有紧接在prev元素后的next元素

$("div + span")//紧接在div后的span

4.prev ~ siblings 匹配prev元素之后的所有siblings元素

$("form ~ input")//找到所有与表单同辈的input元素

选择器-简单
1.:first 匹配找到的第一个元素

$("tr:first")//查找表格中第一行


2.:last 匹配找到的最后一个元素
$("tr:last")//匹配找到的最后一个元素


3.:not(selector) 去除所有与给定选择器匹配的元素
   ps:jquery 1.3中,已支持复杂选择器了(例如::not(div a)和:not(div,a))
   $("input not(:checked)")//所有未被选中的input元素


4.:even 匹配所有索引值为偶数的元素,从0开始计数
   $("tr:even")//查找表格中偶数行


5.:odd匹配所有索引值为奇数的元素,从0开始计数
   $("tr:odd")//查找表格中奇数行


6.:eq(index)匹配一个给定索引值的元素
   $("tr:eq(1)")//查找第二行


7.:gt(index)匹配所有大于给定索引值的元素
   $("tr:gt(0)")//查找大于0的所有行


8.:lt(index)匹配所有小于给定索引值的元素
$("tr:lt(2)")//查找小于2的所有行


9.:header 匹配如h1,h2,h3之类的标题元素
   $(":header").css("background",red");//所有标题加上背景色


10.:animated 匹配所有正在执行动画效果的元素


选择器-内容:
1.:contains(text) 匹配包含给定文本的元素
   $("div:contains('aaa')")查找所有包含有aaa的div元素


2.:empty()匹配所有不包含子元素或文本的空元素
   $("td:empty")


3.:has(selector)匹配含有选择器所匹配的元素的元素
   $("div:has(p)").addClass("test");//给所有包含p元素的div元素添加一个text类


4.:parent匹配含有子元素或者文本的元素
   $("td:parent");//查找所有含有子元素或者文本的td元素


选择器-可见性:
1.:hidden匹配所有不可见元素,input元素的type属性为hidden的话也会被匹配
   $("tr:hidden");//查找所有不可见的tr元素


2.:visible匹配所有可见元素
   $("tr:visible");//查找所有可见的tr元素


选择器-属性:
1.[attribute]匹配包含给定属性的元素
    $("div[id]")//查找所有含有id属性的div元素


2.[attribute=value]匹配给定的属性是某个特定值的元素
    $("input[name='username']")//查所所有name=username的input元素


3. [attribute!=value]匹配所有不含有指定属性,或者属性不等于特定值的元素
     此选择器等价于:not([attr=value]),要匹配含有特定属性但不等于特定值的元素,请使用[attr]:not([attr=value])
    $("input[name!='username']")//查找所有name!=username的input元素


4. [attribute^=value]匹配给定的属性是以某些值开始的元素
    $("input[name^='user'])//查找所有name以'newws'开始的input元素


5. [attribute$=value]匹配给定属性是以某些值结尾的元素
   $("input[name$='letter']) //查找所有name以'letter'结尾的input元素


6. [attribute*=value]匹配给定的属性是以包含某些值的元素
   $("input[name*='man']")//查找所有name包含'man'的input元素


7. [selector1][selector2][selectorN]复合属性选择器,冉要同时满足多个条件时用。
    $("input[id][name='man']")//含有id属性,并且name为man的


选择器-子元素:
1.:nth-child(index/even/odd/equation)匹配其父元素下的第N个子或奇偶元素
    $("ul li:nth-child(2)")//在每个ul查找第2个li


2. :first-child匹配第一个子元素
    $("ul li:first-child")//在每个ul中查找第一个li


3.:las-child匹配最后一个子元素
    $("ul li:last-child")// 在第个ul中查找最后一个li


4.only-child如果某个元素是父元素中唯一的子元素,那将会被匹配,如果父元素中含有其他元素,不会被匹配
   $("ul li:only-child")//在ul中查找是唯一子元素的li


Selector-Form:
1.:input, :text, :password, :radio, :checkbox, :submit, :image, :reset, :button, :file
2. :hidden matches all invisible elements, or elements of type hidden


Selector - form object attribute:
1.:enable matches all available elements
$("input:enabled")//Find all available input elements


2.:disabled matches all unavailable elements
$("input:disabled")//matches all unavailable elements


3.:checked matches all selected selected elements (check boxes, radio boxes, excluding options in select)
$("input:checked")//Find all selected elements Checkbox element


4.:selected matches all selected option elements
$("select option:selected")//Find all selected option elements

Statement
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
JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools