search
HomeWeb Front-endJS TutorialDetailed explanation of jquery selector basic course examples
Detailed explanation of jquery selector basic course examplesJun 17, 2017 pm 02:03 PM
jqueryselectorBaseExamplecourse

Jquery's selector is quite handsome, borrowing the syntax from XPath2.0 and CSS1-3, and is compatible with multiple browsers, making the originally very complex DOM suddenly become simpler, with the latest in hand The version is 1.2.2b, and all the examples below are also examples provided based on this version.

Test HTML code:

<div id="father">
   <div id="first">I am first</div>
   <div id="second" class="red">I am second</div>
   <div id="third" style="display:none">I am third</div>
</div>
<p class="red">I am forth</p>
<h4></h4>

Basic:

#id: According to the id of object PropertiesGet the object.

alert($('#first').html());
/ /Display I am first

element: All objects matching a certain HTML tag

alert($('p').length);
//Display 4

.class : Get the object based on its class attribute

alert($('.red').length);
//Display 2

*: Get all objects

alert( $('*').length);
//Display the sum of objects in HTML, but the results will be different in different browsers

selector1, selector2, selectorN: Get a collection of multiple selectors without removing duplicates.

alert($('.red,#second, p').length);
//Display 4

Level selector:

ancestor descendant: This selector is a space, indicating that the first All objects of one selector, and then find all objects matching the second selector among its descendant nodes.

alert($('#father .red').html());
//Display I am second

parent > child: This selector is the greater than sign, which means that all objects of the first selector are found first, and then in his Find all objects matching the second selector in child nodes (not grandchild nodes).

alert($('#father > .red ').html());
//Display I am second

prev + next: This selector is the plus sign, which means to find all the objects of the first selector first, and then find the same level as him The next node that immediately follows also matches the object of the second selector.

alert($('#father + .red').html());
//Display I am forth

prev ~ siblings: This selector is the ~ sign, which means to first find all the objects of the first selector, and then find them Objects that match the second selector in all subsequent nodes of the same level.

alert($('#first ~ #third').html());
//Display I am third

Basic filter character:

:first: matches the first of multiple objects objects
:last: Match the last object among multiple objects

alert($ ('.red:first').html());
//Display I am second
alert($('p:last') .html());
//Show I am third

:not(selector): Match items that remove the content in the selector after not

alert($('.red:not(#second)'). html());
//Show I am forth

:even: Matches the even number
of all objects: odd: Matches the odd number

of all objects

alert($('p:even').length);
//显示2
alert($('p:odd').length);
//显示2

:eq(index):匹配某一下表的单独某元素

alert($('p:eq(2)').html());
//显示I am second

:gt(index):匹配大于某一下标的所有元素
:lt(index):匹配小于某一下标的所有元素

alert($('p:gt(1)').length);
//显示2
alert($('p:lt(2)').length);
//显示2

:header:匹配所有的header元素,例如h1,h2,h3,h4,h5,h6

alert($(':header').length);
//显示1

:animated:匹配所有有动画效果的元素

function animateIt()
{
   $("#second").slideToggle("slow", animateIt);
}
animateIt();
alert($(&#39;:animated&#39;).html());
//显示I am second

文本过滤符:

:contains(text):匹配内部拥有该文本元素的对象,包含间接有用的情况

alert($('p:contains("first")').length);
//显示2

:empty:匹配所有没有子元素的对象

alert($(':header:empty').length);
//显示1

:has(selector):匹配所有至少含有一个子选择符的对象

alert($('p:has("#third")').attr('id'));
//显示father

:parent:匹配所有的父对象,父对象包含那些只含有文本的对象

alert($('p:parent').length);
//显示4

可见性过滤符:

:hidden:匹配所有隐藏对象,或者input中的hidden类型
:visible:匹配所有可见的对象

alert($('p:hidden').length);
//显示1
alert($('p:visible').length);
//显示3

属性过滤符:

[attribute]:匹配拥有某一属性的所有对象
[attribute=value]:匹配拥有某一属性和值的对象
[attribute!=value]:匹配拥有某一属性,且不是某一值的对象
[attribute^=value]:匹配拥有某一属性,且以某一值开头的对象
[attribute$=value]:匹配拥有某一属性,且以某一值结尾的对象
[attribute*=value]:匹配拥有某一属性,且包含某一值的对象

alert($(&#39;p[class]&#39;).html());
//显示I am second
alert($(&#39;p[class=red]&#39;).html());
//显示I am second
alert($(&#39;p[id!=father]&#39;).length);
//显示3
alert($(&#39;p[id^=f]&#39;).length);
//显示2
alert($(&#39;p[id$=d]&#39;).length);
//显示2
alert($(&#39;p[id*=ir]&#39;).length);
//显示2

[selector1][selector2][selectorN]:匹配同时符合多个属性选择符的对象

alert($('p[id=second][class^=r]').length);
//显示I am second

子过滤符:

:nth-child(index/even/odd/equation):匹配子元素中的某一下标/偶数/奇数/等式的对象,:eq(index)只能匹配某单一对象的子元素特征,而这个方法可以匹配多个对象的某一子元素共同特征

alert($(&#39;#father p:nth-child(1)&#39;).html());
//显示I am first
alert($(&#39;#father p:nth-child(even)&#39;).length);
//显示1
alert($(&#39;#father p:nth-child(odd)&#39;).length);
//显示2
alert($(&#39;#father p:nth-child(3n)&#39;).length);
//显示1,其实是每3个一匹配

:first-child:匹配第一个子元素
:last-child:匹配最后一个子元素
这两个匹配符也可以对多个父对象的所有子元素进行匹配操作

alert($(&#39;#father p:first-child&#39;).html());
//显示I am first
alert($(&#39;#father p:last-child&#39;).html());
//显示I am third

:only-child:如果一个父元素只有一个子元素,就匹配这个子元素

alert($('p:only-child').length);
//显示0

The above is the detailed content of Detailed explanation of jquery selector basic course examples. For more information, please follow other related articles on the PHP Chinese website!

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
jquery实现多少秒后隐藏图片jquery实现多少秒后隐藏图片Apr 20, 2022 pm 05:33 PM

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

jquery怎么修改min-height样式jquery怎么修改min-height样式Apr 20, 2022 pm 12:19 PM

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

axios与jquery的区别是什么axios与jquery的区别是什么Apr 20, 2022 pm 06:18 PM

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

jquery怎么在body中增加元素jquery怎么在body中增加元素Apr 22, 2022 am 11:13 AM

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

jquery怎么删除div内所有子元素jquery怎么删除div内所有子元素Apr 21, 2022 pm 07:08 PM

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

jquery on()有几个参数jquery on()有几个参数Apr 21, 2022 am 11:29 AM

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

jquery怎么去掉只读属性jquery怎么去掉只读属性Apr 20, 2022 pm 07:55 PM

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

jquery怎么获取table当前第几行jquery怎么获取table当前第几行Apr 21, 2022 pm 05:38 PM

方法:1、用“$("tr").click(function(){})”给表格行元素绑定点击事件,并设置处理函数;2、在函数中,用“$(this).index()+1”获取点击元素的行数即可。index()获取值从0开始计数,需进行加1处理。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version