Home  >  Article  >  Web Front-end  >  jquery selector

jquery selector

韦小宝
韦小宝Original
2018-03-14 17:12:431674browse

This article talks about jquery's selector. If you don't know about jquery's selector or are interested in jquery's selector, then let's take a look at this article. Okay, let's stop talking and get to the point. Bar

1.jQuery basic selector
##
#id
根据给定id匹配一个元素
.class 
根据给定class匹配一个元素 
element 根据给定元素名匹配元素
*       匹配所有的元素
selector1,selector2.,···,selectorN将每一个选择器匹配到的元素合并后一起返回

2. Hierarchical selector
$(“parent child”)  
 
选择parent中所有的child元素
$(“parent >child”) 
选择parent中的子元素,而上面的是选择所有后代元素
$(“prev+next”) 
选择紧跟在prev元素后的next元素(同辈)
注意:等价于 next()函数,即$(“.one”).next(“p”)
$(“prev~siblings”) 
选择prev元素之后的所有siblings元素(同辈)  
注意:等价于nextAll()函数,即$(“.one”).nextAll(“p”)

3. Filter selector

a.Basic filter selector
:first 
选择第一个元素 $(“p:first”)选择所有p元素中的第一个p元素,
与下面的first-child有区别
:last 
选择最后一个元素,原理同上
:not(selector)
 
去除所有与给定选择器匹配的元素
:even 
选择索引是偶数的所有元素
注意:索引从0开始
:odd 
选择索引是奇数的所有元素
:eq(index)
 
选择索引等于index的元素
注意:index从0开始
:gt(index)
 
选择索引大于index的元素
:lt(index)
 
选择索引小于index的元素
:header
 
选择所有的标题元素,如h1,h2等等
:animated 
选择正在执行动画的元素
:focus 
选择当前获取焦点的元素

b. Content Filter
##
:contains(text)
选择含有文本内容为text的元素
:empty 
选择不包含子元素或者文本的元素
:has(selector)
选择含有选择器所匹配的元素的元素
:parent
选择含有子元素或者文本的元素(
这个比较绕)

c. Visibility Filter Selector

:hidden 
选择所有不可见的元素,
这个需要注意的是:它不仅包括样式属性display为“none”的元素,也包括文本隐藏域(<input type=“hidden” />)和visibility:hidden之类的元素
:visible 
选择所有可见元素

d. Attribute filter selector

【attribute】 
选择拥有此属性的元素
【attribute = value】   选择属性值为value的元素
【attribute !=value】 
选择属性值不为value的元素
【attribute^=value】 
选择属性值以value开始的元素
【attribute $=value】 
选择属性值以value结束的元素
【attribute *=value】 
选择属性值含有value的元素
【attribute |=value】 
选择属性等于给定字符串或者以该字符串为前缀(该字符串后跟一个连字符“-”)的元素
【attribute ~=value】 
选择属性用空格分隔的值中包含一个给定值的元素
【attribute1】【attribute2】【attributeN】用属性选择器合并成一个复合属性选择器,满足多个条件,没选择一次,缩小一次范围

e. Child element filter selector

:nth-child(index/even/odd/eq)
:eq(index)只会匹配一个元素,而:nth-child将为每一个父元素匹配子元素,
并且:nth-child(index)的index是从1开始的,:eq(index)是从0开始的
:first-child
 
选择每个父元素的第一个子元素,
:first只会返回单个元素,而:first-child将会为每个父元素匹配第一个子元素
:last-child
 
原理同上
:only-child 如果某个元素是它父元素中唯一的子元素,那么就会匹配
例如:$(“ul li:only-child”)在ul中选择有唯一子元素的li元素

f. Form object attribute filter selector


:enabled
 
选择所有可用元素
:disabled
 
选择所有不可用元素
:checked
 
选择所有被选中的元素,包括单选和复选
:selected
 
选择所有被选中的选项元素(下拉列表)

4. Form selector


:input 
选择所有的表单元素
 包括input、textarea、select、button元素
例如:$(“#form :input”)选择所有的表单元素 与 $(“#form input”)只获取input元素
:text 
选择所有单行文本框
:password 
选择所有密码框
:radio 
选择所有单选框
:checkbox 
选择所有多选框
:submit 
选择所有提交按钮
:image 
选择所有图像按钮
:reset 
选择所有重置按钮
:button 
选择所有按钮
:file 
选择所有的上传域
:hidden 
选择所有不可见元素

The above selectors can basically meet the needs of daily development and use. If you don’t know much about it, you can implement both sides yourself and it will be easy to master!


Related recommendations: Detailed explanation of jQuery selector How to deal with special symbols

What types of JQuery selectors are there

The above is the detailed content of jquery selector. 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