jquery 选择器

本文内容   


    jQuery提供了两个方法来选择元素。第一个是使用一个CSS和XPath选择器的结合作为一个字符串被传递给jQuery构造器。第二种方法是使用jQuery对象的一些方法。这两个事可以结合使用的。。
Lk)K6['g/J{PTG0                                                                                         
fQ:N(ni:A.b0/]'B#Pk)W!}rLB%h3}0   对于每个onxxx事件,象onclick,onchange,onsubmit等,都有一个和jquery同意义的对应的事件,而向ready,hover等,都是为某个方法提供。PHPChina 开源社区门户3s4u;e|KR&w@b
                                                                                          PHPChina 开源社区门户t4`9^(_[`7f:o7m
  还有jquery特有的,把一个选择器的所有事件并排列出来,中间用"."隔开(有点像对象方法的调用 ,特别提醒:每次方法调用后,返回本对象的引用,即选择器选择的对象):

"w/ee-o?iB1h}y!L0        $("#orderedlist").find("li").each(function(i) {PHPChina 开源社区门户7rO[J6~?d%_#bO
            $(this).html( $(this.html() + " BAM! " + i);
'o;p OR4P;F-WC OI0        });



jQuery1.2选择器  
绯雨汉化:http://feiyu.asgard.cn

基本选择器

#myid返回: <jQuery对象>
匹配一个id为myid的元素。
element返回: <jQuery对象> 数组
匹配所有的element元素
.myclass返回: <jQuery对象> 数组
匹配所有class为myclass的元素
*返回: <jQuery对象> 数组
匹配所有元素。该选择器会选择文档中所有的元素,包括html,head,body
selector1,selector2,selectorN返回: <jQuery对象> 数组
匹配所有满足selector1或selector2或selectorN的元素

层次选择

elementParent elementChild返回: <jQuery对象> 数组
匹配elementParent下的所有子元素elementChild。例如:$("div p") 选择所有div下的p元素
elementParent > elementChild 返回: <jQuery对象> 数组
匹配elementParent下的子元素elementChild。例如:$("div>p") 选择所有上级元素为div的p元素
prev+next返回: <jQuery对象> 数组
匹配prev同级之后紧邻的元素next。例如:$("h1+div") 选择所有div同级之前为h1的元素(<h1 /><div />)
prev ~ siblings返回: <jQuery对象> 数组
匹配prev同级之后的元素siblings。例如:$("h1~div") 可以匹配(<h1 /><div /><div />)

基本滤镜

:first返回: <jQuery对象>
匹配第一个元素
:last返回: <jQuery对象>
匹配最后一个元素
:not(selector)返回: <jQuery对象> 数组
匹配不满足selector的元素
:has(selector)返回: <jQuery对象> 数组
匹配包含满足selector的元素。此选择器为1.2新增
:even返回: <jQuery对象> 数组
从匹配的元素集中取序数为偶数的元素。
:odd返回: <jQuery对象> 数组
从匹配的元素集中取序数为奇数的元素。
:eq(index)返回: <jQuery对象> 数组
从匹配的元素集中取第index个元素
:gt(index)返回: <jQuery对象> 数组
从匹配的元素中取序数大于index的元素
:lt(index)返回: <jQuery对象> 数组
从匹配的元素中取序数小于index的元素
:header返回: <jQuery对象> 数组
匹配所有的标题元素,例如h1,h2,h3……hN。此选择器为1.2新增
:animated返回: <jQuery对象> 数组
匹配正在执行动画的元素。此选择器为1.2新增
:empty返回: <jQuery对象> 数组
匹配所有没有子元素(包括文本内容)的元素
:parent返回: <jQuery对象> 数组
匹配包含子元素(包含文本内容)的所有元素
:contains(text)返回: <jQuery对象> 数组
匹配所有含有text的元素
:hidden返回: <jQuery对象> 数组
匹配所有隐藏的元素,包含属性type值为hidden的元素
:visible返回: <jQuery对象> 数组
匹配所有非隐藏的元素

子元素滤镜

E:nth-child(index/even/odd/equation)返回: <jQuery对象> 数组
匹配所有E在其父元素下满足(index/even/odd/equation)条件的集合。注:下标从1开始
E:first-child返回: <jQuery对象> 数组
匹配所有E在其父元素下是第一个子元素的集合。例 如:HTML(<div><p id="p1"></p></div><div><p id="p2"></p><p id="p3"></p></div>"),使用$("p:first-child"),选取:<p id="p1"></p><p id="p2"></p>
E:last-child返回: <jQuery对象> 数组
匹配所有E在其父元素下是最后一个子元素的集合。例如:同上的HTML,使用$("p:last-child"),选取:<p id="p1"></p><p id="p3"></p>
E:only-child返回: <jQuery对象> 数组
匹配所有E是其父元素的唯一子元素的集合。例如:同上的HTML,使用$("p:only-child"),选取:<p id="p1"></p>

表单滤镜

:input返回: <jQuery对象> 数组
匹配所有的input、textarea、select、button
:text返回: <jQuery对象> 数组
匹配文本域。注:在IE浏览器下,选择的对象是所有type属性为text的元素,在非IE浏览器下,选择的对象是input元素type属性为text的元素
:password返回: <jQuery对象> 数组
匹配密码域。注:在IE浏览器下,选择的对象是所有type属性为password的元素,在非IE浏览器下,选择的对象是input元素type属性为password的元素
:radio返回: <jQuery对象> 数组
匹配单选按钮。注:在IE浏览器下,选择的对象是所有type属性为radio的元素,在非IE浏览器下,选择的对象是input元素type属性为radio的元素
:checkbox返回: <jQuery对象> 数组
匹配复选框。注:在IE浏览器下,选择的对象是所有type属性为checkbox的元素,在非IE浏览器下,选择的对象是input元素type属性为checkbox的元素
:submit返回: <jQuery对象> 数组
匹配提交按钮。注:在IE浏览器下,选择的对象是所有type属性为submit的元素,在非IE浏览器下,选择的对象是input元素type属性为submit的元素和button元素type属性为空或为submit的元素
:image返回: <jQuery对象> 数组
匹配图像域。注:在IE浏览器下,选择的对象是所有type属性为image的元素,在非IE浏览器下,选择的对象是input元素type属性为image的元素
:reset返回: <jQuery对象> 数组
匹配重置按钮。注:在IE浏览器下,选择的对象是所有type属性为reset的元素,在非IE浏览器下,选择的对象是input或button元素type属性为reset的元素
:button返回: <jQuery对象> 数组
匹配按钮。注:在IE浏览器下,选择的对象是所有type属性为button的元素和元素名为button的元素,在非IE浏览器下,选择的对象是input元素type属性为button的元素和元素名为button的元素
:file返回: <jQuery对象> 数组
匹配文件域。注:在IE浏览器下,选择的对象是所有type属性为file的元素,在非IE浏览器下,选择的对象是input元素type属性为file的元素
:enabled返回: <jQuery对象> 数组
匹配所有可用的元素。注:即:not(:disabled),参考:disabled的注释
:disabled返回: <jQuery对象> 数组
匹配所有禁用的元素。注:在非IE浏览器下,选择的对象是禁用的表单元素
:checked返回: <jQuery对象> 数组
匹配所有被选中的表单。注:在IE浏览器下,选择的对象是含有checked属性的所有元素
:selected返回: <jQuery对象> 数组
匹配所有选择的表单。注:在IE浏览器下,选择的对象是含有selected属性的所有元素

属性滤镜

[attribute]返回: <jQuery对象> 数组
匹配拥有attribute属性的元素
[attribute=value]返回: <jQuery对象> 数组
匹配属性attribute为value的元素
[attribute!=value]返回: <jQuery对象> 数组
匹配属性attribute不为value的元素
[attribute^=value]返回: <jQuery对象> 数组
匹配属性attribute的值以value开始的元素
[attribute$=value]返回: <jQuery对象> 数组
匹配属性attribute的值以value结尾的元素
[attribute*=value]返回: <jQuery对象> 数组
匹配属性attribute的值包含value的元素
[selector1][selector2][selectorN]返回: <jQuery对象> 数组
匹配满足属性选择器selector1、selector2、selectorN的元素

css 选择器   
*a*n`$OH,| q0  既然选择器支持css 标准,以下是css选择器的参数列表,同样适应jquery。

PatternMeaningDescribed in sectionFirst defined in CSS level
*any elementUniversal selector2
Ean element of type EType selector1
E[foo]an E element with a "foo" attributeAttribute selectors2
E[foo="bar"]an E element whose "foo" attribute value is exactly equal to "bar"Attribute selectors2
E[foo~="bar"]an E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "bar"Attribute selectors2
E[foo^="bar"]an E element whose "foo" attribute value begins exactly with the string "bar"Attribute selectors3
E[foo$="bar"]an E element whose "foo" attribute value ends exactly with the string "bar"Attribute selectors3
E[foo*="bar"]an E element whose "foo" attribute value contains the substring "bar"Attribute selectors3
E[hreflang|="en"]an E element whose "hreflang" attribute has a hyphen-separated list of values beginning (from the left) with "en"Attribute selectors2
E:rootan E element, root of the documentStructural pseudo-classes3
E:nth-child(n)an E element, the n-th child of its parentStructural pseudo-classes3
E:nth-last-child(n)an E element, the n-th child of its parent, counting from the last oneStructural pseudo-classes3
E:nth-of-type(n)an E element, the n-th sibling of its typeStructural pseudo-classes3
E:nth-last-of-type(n)an E element, the n-th sibling of its type, counting from the last oneStructural pseudo-classes3
E:first-childan E element, first child of its parentStructural pseudo-classes2
E:last-childan E element, last child of its parentStructural pseudo-classes3
E:first-of-typean E element, first sibling of its typeStructural pseudo-classes3
E:last-of-typean E element, last sibling of its typeStructural pseudo-classes3
E:only-childan E element, only child of its parentStructural pseudo-classes3
E:only-of-typean E element, only sibling of its typeStructural pseudo-classes3
E:emptyan E element that has no children (including text nodes)Structural pseudo-classes3
E:link
E:visited
an E element being the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited)The link pseudo-classes1
E:active
E:hover
E:focus
an E element during certain user actionsThe user action pseudo-classes1 and 2
E:targetan E element being the target of the referring URIThe target pseudo-class3
E:lang(fr)an element of type E in language "fr" (the document language specifies how language is determined)The :lang() pseudo-class 2
E:enabled
E:disabled 
a user interface element E which is enabled or disabledThe UI element states pseudo-classes3
E:checked
E:indeterminate 
a user interface element E which is checked or in an indeterminate state (for instance a radio-button or checkbox)The UI element states pseudo-classes3
E:contains("foo")an E element containing the substring "foo" in its textual contentsContent pseudo-class3
E::first-linethe first formatted line of an E elementThe :first-line pseudo-element1
E::first-letterthe first formatted letter of an E elementThe :first-letter pseudo-element1
E::selectionthe portion of an E element that is currently selected/highlighted by the userThe UI element fragments pseudo-elements3
E::beforegenerated content before an E elementThe :before pseudo-element2
E::aftergenerated content after an E elementThe :after pseudo-element2
E.warningan E element whose class is "warning" (the document language specifies how class is determined).Class selectors1
E#myidan E element with ID equal to "myid".ID selectors1
E:not(s)an E element that does not match simple selector sNegation pseudo-class3
E Fan F element descendant of an E elementDescendant combinator1
E > Fan F element child of an E elementChild combinator2
E + Fan F element immediately preceded by an E elementDirect adjacent combinator2
E ~ Fan F element preceded by an E elementIndirect adjacent combinator3


  部分解释


  *匹配所有元素:  $("*") PHPChina 开源社区门户8s!ulOU;DN4Dq9u
  指定任意多个选择器: $("div,span,p,Myclass")
-O1a9h p7]h M8j0  在给定的祖先元素下匹配所有的后代元素: PHPChina 开源社区门户YGL3SJ0@|"t F0w
  找到表单中所有的input元素:$("form input") PHPChina 开源社区门户'Z/zNi,K K3s
  在给定的父元素下匹配所有的子元素: $("form >input")
o�DGX9QE0  匹配所有紧接在label元素后的next元素: $("label + input")
zgx$v /jfV0  匹配找到的第一个元素: $("tr:first")
#Y-[no Q$m p%}7O0  去除所有与给定选择器匹配的元素: $("input:not(checked)") PHPChina 开源社区门户*@,UvG3pg/I4u:J
  匹配所有索引值为偶数的元素(从0开始): $("tr:even")
]$IgI si0  与上相反,匹配索引值为奇数: $("tr:odd") PHPChina 开源社区门户$S%m(s7Nl:|n
  匹配一个给定索引值的元素: $("tr:eq(1)") PHPChina 开源社区门户:~0?4Y/X%N
  匹配索引大于给定索引值的元素: $("tr:gt(0)")
  匹配其父元素下的第N个子或奇偶元素 $("tr:nth-child(index/even/odd/equation)")
            ':eq(index)' 只匹配一个元素,而这个将为每一个父元素匹配子元素。
            :nth-child从1开始的,而:eq()是从0算起的! 可以使用:
     :nth-child(even)
     :nth-child(odd)
     :nth-child(3n)
     :nth-child(2)
     :nth-child(3n+1)
     :nth-child(3n+2) PHPChina 开源社区门户#n O"BT//
  与上相反小于索引值: $("tr:lt(2)") PHPChina 开源社区门户srfk;Q#D
  匹配如h1,h2,h3之类的标题元素: $(":header").css("background","red")
"MWy0S1ASS0  匹配所有正在执行动画效果的元素 PHPChina 开源社区门户.d p hW(hg{[QV
   <button id="run">Run</button><div></div> PHPChina 开源社区门户bgs3Ih1Z0a-N.S
   $("#run").click(function() {
"p ]D2j ?0        $("div:not(:animated)").animate(left:"+20"},1000);
v:P hE'RII(f0   }); PHPChina 开源社区门户g6T}7D4C
  匹配所有不可见元素(包括input的type="hidden"): $("tr:hidden") PHPChina 开源社区门户4/M,{O6b(o;/{
  匹配所有可见元素: $("tr:visible") PHPChina 开源社区门户0Bw�mx#|2zW]`VNC7W
PHPChina 开源社区门户,z|ojd!^U
  一些简单应用实例:
|;w;g/9[ h01.列表有一个ID="orderedlist",给list添加一样式
EG['H /4d @Q4h?0        $("#orderedlist").addClass("red");
l;JJ�H A+/ E~w M1~0 若为class="myClass" PHPChina 开源社区门户6|2I1Z/J"X)aY?1Qf
        $(".myClass").addClass("blue"); PHPChina 开源社区门户$/ f8`r+}3Q1D2T
                                                                                         
B+L v1Z4l q/h*Kc02.为orderedlist的所有子标签li添加一class.blue的样式 PHPChina 开源社区门户` aL%{b
        $("#orderedlist > li").addClass("blue");
;E5tj Ve:k0                                                                                          PHPChina 开源社区门户0V3/(WD_:sq
3.当鼠标移动li元素时,增加和删除一个Class样式: PHPChina 开源社区门户�`6k5B@(I^V&`*H
        $("#orderedlist li:last").hover(function() {
8G$]QOf2SB0            $(this).addClass("green");
^,nnZ DU0B3m,i0        }, function() {
8l%Q&j6K N&c8JKc0            $(this).removeClass("green");
8p.RD(X4t0        });
dYg"~v!Kn3^8TX0                                                                                          PHPChina 开源社区门户 f.g7j;H#?U7S+}
4.多个表单执行reset()方法:
6`'k5ABn ~0        $("#reset").click(function() {
3Nm1D6A4p"n1^&I%e0            $("form").each(function() {
al1Xie;yU D0                this.reset();
+E/e8bN&pl#E ~0            }); PHPChina 开源社区门户ya P.Q/Qz~0M
        }); PHPChina 开源社区门户R#P rRR$Sh*e~5j
                                                                                         
*G4@x#_~05.选择所有的li元素且不包含ul子元素
cW/Qj^!U7V8Sdv0        $("li").not("[ul]").css("border","1px solid black"); PHPChina 开源社区门户9Y,QO1Md Nu
                                                                                         
g.o./2B:w Cr1A/A06.选择包含有name属性的a标签 PHPChina 开源社区门户R%@^ P9YlW
        $("a[@name]").background("red");7.对于FAQ的隐藏与显示 PHPChina 开源社区门户D5p+T!UNRMf2v
  'dd'和'dt'都是#faq的子元素,find():找到它的子元素,end()和next()搭配,end() PHPChina 开源社区门户:v]OD3Q.q4UN
实质上是把'dd'过滤了,也就是next()时实质上是参考的'dt',这样每个'dt'的next就
n#Sp1y6DL9ZC0是'dd'.
5a/}?s9N,pR0        $('#faq').find('dd').hide().end().find('dt').click(function() { PHPChina 开源社区门户KuI yvK Q1}$s
            var answer = $(this).next(); PHPChina 开源社区门户YR+s4zYV8b6X_W7D5V+?
            if(answer.is(':visible')) {
/ji?G4r!R)|l0                answer.slideUp();
4``!kXvb }/L7r1G0            else PHPChina 开源社区门户i:y's(Q3s O3id
                answer.slideDown();
0y-poP9m"Ah0            } PHPChina 开源社区门户8M WtQ O0O
        }); PHPChina 开源社区门户 R,@:mp8nF
                                                                                         
f9EH2l+i&| Y8x08.除同属元素外,我们也可选择父元素(在此,p就是a的父元素): PHPChina 开源社区门户!F*f8F$rNB
        $("a").hover(function() {
Kr[/$y|0t.J kD0          $(this).parents("p").addClass("highlight");
~k.^;g9c G%T;b�v5S0        }, function() { PHPChina 开源社区门户 xK$QU+](Le
          $(this).parents("p").removeClass("highlight");
$|7J8Z!xh"~5|:@0        });
h|N-Z^k*|c�y0
od4wI%Y:f2Q U6h s0插件的制作: PHPChina 开源社区门户 vHHm^(p!o1]h-O0}

~r^Bl(LW0 插件:写自己的jquery插件 PHPChina 开源社区门户+N6r/f#DgS
  1.给你的插件命名,比如叫 "footbar" PHPChina 开源社区门户z!WJ(@OX{BT
  2.创建一个文件名为:jquery.[插件名].js 如:jquery.footbar.js PHPChina 开源社区门户0|3Bm$c#A"IYE1?"i&T
  3.扩展jquery的内部 对象,创建一个或多个插件的方法,如
m.lv H6iL D0C�h0   jQuery.fn.footbar = function() {
'{mus:Abh.Z)c;F7p0        //do something
$y1OX%PO8R5[t!i0   }); PHPChina 开源社区门户8@EUbOEb W
                                                                                         
*aE5_U8}U./0  4.用帮助函数创建一个对象(可选)
y&i#G;/hw0    jQuery.footbar = { PHPChina 开源社区门户'oza`;iX
        height: 5,
p7rE,m4C%Lp(MV"gh-L0        calculateBar = function() {...},
xS"b+nj;E,bc0        checkDependencies = function() {...} PHPChina 开源社区门户zOx:tl*d] g
    }; PHPChina 开源社区门户p+h I*S1m8d{,G!t2}V
                                                                                          PHPChina 开源社区门户[x7PM"uA({+/.K)H
  然后你能在你的插件里调用这些帮助函数
sy%dJ0n0N0cO-Yo p0    jQuery.fn.footbar = function() {
[TE&NL/O4q0        //do something
3f&LV w2l z3FILv&J0    jQuery.footbar.checkDependencies(value); PHPChina 开源社区门户 tpvDQ#@
    //do something PHPChina 开源社区门户6o+e |"NT:YO7Q
    };
aXCG u0                                                                                          PHPChina 开源社区门户-z]#jPks;m$@#r
  5.创建一个用户能修改的默认设置(可选)
&h2zF5Vr�?8v0   jQuery.fn.footbar = function(options) { PHPChina 开源社区门户ZP;I9r�E-k?
        var settings = {
u7O~,E:l;]l'a0           value: 5, PHPChina 开源社区门户Wh6CwD0`,sG4k
           name: "pete", PHPChina 开源社区门户-F9JM2b n,B9c
           bar: 655
L@@,za!N,H_}0        };
MblY&w4y+m�[4V0        if(options) {
(v hW(hn,TB_Q!X0           jQuery.extend(settings,options);
/cD/.I8C,CjSg0        } PHPChina 开源社区门户 d.X2e5Z3i.T
   }; PHPChina 开源社区门户U,T5I |}O
  然后你就能用下面的默认调用该插件(有选项)
6W*o6DG!b0   $("...").footbar({
4~z@y6PY0        value:123, PHPChina 开源社区门户v8]dwPp
        bar:9 PHPChina 开源社区门户*RY o8s}
   }); PHPChina 开源社区门户LE S4SOz
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值