Jquery選擇器學習(1)--基本

Jquery選擇器, 最基本的:

1.   #id : 据给定的ID匹配一个元素。

如果选择器中包含特殊字符,可以用两个斜杠转义

HTML 代码:

<div id="myDiv">id="myDiv"</div>

<span id="foo:bar"></span>

<span id="foo[bar]"></span>

< span id="foo.bar"></span>

jQuery 代码:

$("#myDiv")

$("#foo\\:bar")

$("#foo\\[bar\\]")

$("#foo\\.bar")

2.   element : $("div")

* : 匹配所有元素 如 $("*")

3.   .Class: 根据给定的类匹配元素

如 $(".myClass")

綜合使用以上: 如 $("div,span,p.myClass")

4.   $(elements):将一个或多个DOM元素转化为jQuery对象。

$(document.body).css( "background", "black" );

$(myForm.elements).hide()

5.   $(expression, [context]):context 中查找符合expression的元素集合

Context:作为待查找的 DOM 元素集、文档或 jQuery 对象。

$("input:radio", document.forms[0]);

$("div", xml.responseXML);

$("input:checkbox", "#chkHRCategory")

6.   $(ancestor descendant): 在给定的祖先元素下匹配所有的后代元素

$("#chkHRCategory input:checkbox")

$("form input")

7.   $(parent > child): 在给定的父元素下匹配所有的子元素

$("form > input")

8.   $(prev + next): 匹配所有紧接在 prev 元素后的 next 元素

$("label + input")

9.   $(prev ~ siblings): 匹配 prev 元素之后的所有同輩元素

$("label ~ input")

10.  屬性

[attribute!=value]:不相等

[attribute=value]: 相等

[attribute$=value]: 以value結尾

[attribute^=value]: 以value 開頭

[attribute*=value]: 包含value

[attribute]: 匹配包含给定属性的元素

多個屬性選擇器:$("input[id][name$='man']")

11.  :enabled: 匹配所有可用元素

$("input:enabled")

12.  :disabled: 匹配所有不可用元素

$("input:disabled")

13.  :checked: 匹配所有选中的被选中元素(复选框、单选框等,不包括select中的option)

$("input:checked")

14.  :selected: 匹配所有选中的option元素

$("select option:selected")

 

posted @ 2013-03-14 15:33  邪见  阅读(158)  评论(0)    收藏  举报