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
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($(':animated').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($('p[class]').html()); //显示I am second alert($('p[class=red]').html()); //显示I am second alert($('p[id!=father]').length); //显示3 alert($('p[id^=f]').length); //显示2 alert($('p[id$=d]').length); //显示2 alert($('p[id*=ir]').length); //显示2
[selector1][selector2][selectorN]:匹配同时符合多个属性选择符的对象
alert($('p[id=second][class^=r]').length);
//显示I am second
子过滤符:
:nth-child(index/even/odd/equation):匹配子元素中的某一下标/偶数/奇数/等式的对象,:eq(index)只能匹配某单一对象的子元素特征,而这个方法可以匹配多个对象的某一子元素共同特征
alert($('#father p:nth-child(1)').html()); //显示I am first alert($('#father p:nth-child(even)').length); //显示1 alert($('#father p:nth-child(odd)').length); //显示2 alert($('#father p:nth-child(3n)').length); //显示1,其实是每3个一匹配
:first-child:匹配第一个子元素
:last-child:匹配最后一个子元素
这两个匹配符也可以对多个父对象的所有子元素进行匹配操作
alert($('#father p:first-child').html()); //显示I am first alert($('#father p:last-child').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!

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

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

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

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

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

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

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

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


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

Dreamweaver Mac version
Visual web development tools

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
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version