2008-12-19 JQuery 学习 Vol.2
时间:2008-12-19 来源:Givemefive
1. JQuery Selector
1.1 使用基本的css selector
■ a—This selector matches all link (<a>) elements.
■ #specialID—This selector matches elements that have an id of specialID.
■ .specialClass—This selector matches elements that have the class of specialClass.
■ a#specialID.specialClass—This selector matches links with an id of specialID
and a class of specialClass.
■ p a.specialClass—This selector matches links with a class of specialClass
declared within <p> elements.
1.2 The basic CSS Selectors supported by jQuery
* Matches any element.
E Matches all element with tag name E.
E F Matches all elements with tag name F that are descendents of E.
E>F Matches all elements with tag name F that are direct children of E.
E+F Matches all elements F immediately preceded by sibling E.
E~F Matches all elements F preceded by any sibling E.
E:has(F) Matches all elements with tag name E that have at least one descendent with tag name F.
E.C Matches all elements E with class name C. Omitting E is the same as *.C.
E#I Matches element E with id of I. Omitting E is the same as *#I.
E[A] Matches all elements E with attribute A of any value.
E[A=V] Matches all elements E with attribute A whose value is exactly V.
E[A^=V] Matches all elements E with attribute A whose value begins with V.
E[A$=V] Matches all elements E with attribute A whose value ends with V.
E[A*=V] Matches all elements E with attribute A whose value contains V.
1.3 The more advanced positional selectors supported by jQuery: selecting elements based on their position in the DOM
:first The first match of the page. li a:first returns the first link also under
a list item.
:last The last match of the page. li a:last returns the last link also under
a list item.
:first-child The first child element. li:first-child returns the first item
of each list.
:last-child The last child element. li:last-child returns the last item
of each list.
:only-child Returns all elements that have no siblings.
:nth-child(n) The nth child element. li:nth-child(2) returns the second list item of each list.
:nth-child(even|odd) Even or odd children. li:nth-child(even) returns the even children of each list.
:nth-child(Xn+Y) The nth child element computed by the supplied formula. If Y is 0, it may be omitted. li:nth-child(3n) returns every third item, whereas
li:nth-child(5n+1) returns the item after every fifth element.
:even and :odd Even and odd matching elements page-wide. li:even returns every even
list item.
:eq(n) The nth matching element.
:gt(n) Matching elements after (and excluding) the nth matching element.
:lt(n) Matching elements before (and excluding) the nth matching element.
1.4 The jQuery custom filter selectors that give immense power to identify target elements
:animated Selects elements that are currently under animated control. Chapter 5 will cover animations and effects.
:button Selects any button (input[type=submit], input[type=reset],
input[type=button], or button).
:checkbox Selects only check box elements (input[type=checkbox]).
:checked Selects only check boxes or radio buttons that are checked (supported by CSS).
:contains(foo) Selects only elements containing the text foo.
:disabled Selects only form elements that are disabled in the interface (supported by CSS).
:enabled Selects only form elements that are enabled in the interface (supported by CSS).
:file Selects all file elements (input[type=file]).
:header Selects only elements that are headers; for example: <h1> through
<h6> elements.
:hidden Selects only elements that are hidden.
:image Selects form images (input[type=image]).
:input Selects only form elements (input, select, textarea, button).
:not(filter) Negates the specified filter.
:parent Selects only elements that have children (including text), but not empty elements.
:password Selects only password elements (input[type=password]).
:radio Selects only radio elements (input[type=radio]).
:reset Selects reset buttons (input[type=reset] or button[type=reset]).
:selected Selects option elements that are selected.
:submit Selects submit buttons (button[type=submit] or input[type=submit]).
:text Selects only text elements (input[type=text]).
:visible Selects only elements that are visible.
2. 操作 selector set
2.1 size
size()
Returns the count of elements in the wrapped set
Parameters
none
Returns
The element count
2.2 get
get(index)
Obtains one or all of the matched elements in the wrapped set. If no parameter is specified,all elements in the wrapped set are returned in a JavaScript array. If an index parameter is provided, the indexed element is returned.
Parameters
index (Number) The index of the single element to return. If omitted, the entire set is returned in an array.
Returns
A DOM element or an array of DOM elements.
2.3 index
index(element)
Finds the passed element in the wrapped set and returns its ordinal index within the set. If the element isn’t resident in the set, the value -1 is returned.
Parameters
element (Element) A reference to the element whose ordinal value is to be determined.
Returns
The ordinal value of the passed element within the wrapped set or -1 if not found.
2.4 add
add(expression)
Adds elements, specified by the expression parameter, to the wrapped set. The expression can be a selector, an HTML fragment, a DOM element, or an array of DOM elements.
Parameters
expression (String|Element|Array) Specifies what is to be added to the matched set.This parameter can be a jQuery selector, in which case any matched
elements are added to the set. If an HTML fragment, the appropriate elements are created and added to the set. If a DOM element or an array of DOM elements, they are added to the set.
Returns
The wrapped set.
2.5 not
not(expression)
Removes elements from the matched set according to the value of the expression parameter.If the parameter is a jQuery filter selector, any matching elements are removed. If an element reference is passed, that element is removed from the set.
Parameters
expression (String|Element|Array) A jQuery filter expression, element reference, or array of element references defining what is to be removed from the
wrapped set.
Returns
The wrapped set.
2.6 filter
filter(expression)
Filters out elements from the wrapped set using a passed selector expression, or a filtering function.
Parameters
expression (String|Function) Specifies a jQuery selector used to remove all elements that do not match from the wrapped set, or a function that makes the
filtering decision. This function is invoked for each element in the set, with the current element set as the function context for that invocation. Any element that returns an invocation of false is removed from the set.
Returns
The wrapped set.
2.7 slice
slice(begin,end)
Creates and returns a new wrapped set containing a contiguous portion of the matched set.
Parameters
begin (Number) The zero-based position of the first element to be included in the returned slice. end (Number) The optional zero-based index of the first element not to be included in the returned slice, or one position beyond the last element to be included. If omitted, the slice extends to the end of the set.
Returns
The newly created wrapped set.
2.8 Methods to obtain new wrapped set based on relationships
children() Returns a wrapped set consisting of all unique children of the wrapped elements.
contents() Returns a wrapped set of the contents of the elements, which may include text nodes, in the wrapped set. (Frequently used to obtain the contents of <iframe> elements.)
next() Returns a wrapped set consisting of all unique next siblings of the wrapped elements.
nextAll() Returns a wrapped set containing all the following siblings of the wrapped elements.
parent() Returns a wrapped set consisting of the unique direct parents of all wrapped elements.
parents() Returns a wrapped set consisting of the unique ancestors of all wrapped elements. This includes the direct parents as well as the remaining ancestors all the way up to, but not including, the document root.
prev() Returns a wrapped set consisting of all unique previous siblings of the wrapped elements.
prevAll() Returns a wrapped set containing all the previous siblings of the wrapped elements.
siblings() Returns a wrapped set consisting of all unique siblings of the wrapped elements.
2.9 find
find(selector)
Returns a new wrapped set containing all elements of the original set that match the passed selector expression.
Parameters
selector (String) A jQuery selector that elements must match to become part of the returned set.
Returns
The newly created wrapped set.
2.10 contains
contains(text)
Returns a new wrapped set composed of elements that contain the text string passed as the text parameter
Parameters
text (String) The text that an element must contain in order to be added to the returned set
Returns
The newly created wrapped set
2.11 is
is(selector)
Determines if any element in the wrapped set matches the passed selector expression
Parameters
selector (String) The selector expression to test against the elements of the wrapped set
Returns
true if at least one element matches the passed selector; false if not
2.12 end
end()
Used within a chain of jQuery command to back up the wrapped set to a previously returned set
Parameters
none
Returns
The previous wrapped set
2.13 andSelf
andSelf()
Merges the two previous wrapped sets in a command chain
Parameters
none
Returns
The merged wrapped set
来自:Manning.jQuery.in.Action.Feb.2008.pdf
1.1 使用基本的css selector
■ a—This selector matches all link (<a>) elements.
■ #specialID—This selector matches elements that have an id of specialID.
■ .specialClass—This selector matches elements that have the class of specialClass.
■ a#specialID.specialClass—This selector matches links with an id of specialID
and a class of specialClass.
■ p a.specialClass—This selector matches links with a class of specialClass
declared within <p> elements.
1.2 The basic CSS Selectors supported by jQuery
* Matches any element.
E Matches all element with tag name E.
E F Matches all elements with tag name F that are descendents of E.
E>F Matches all elements with tag name F that are direct children of E.
E+F Matches all elements F immediately preceded by sibling E.
E~F Matches all elements F preceded by any sibling E.
E:has(F) Matches all elements with tag name E that have at least one descendent with tag name F.
E.C Matches all elements E with class name C. Omitting E is the same as *.C.
E#I Matches element E with id of I. Omitting E is the same as *#I.
E[A] Matches all elements E with attribute A of any value.
E[A=V] Matches all elements E with attribute A whose value is exactly V.
E[A^=V] Matches all elements E with attribute A whose value begins with V.
E[A$=V] Matches all elements E with attribute A whose value ends with V.
E[A*=V] Matches all elements E with attribute A whose value contains V.
1.3 The more advanced positional selectors supported by jQuery: selecting elements based on their position in the DOM
:first The first match of the page. li a:first returns the first link also under
a list item.
:last The last match of the page. li a:last returns the last link also under
a list item.
:first-child The first child element. li:first-child returns the first item
of each list.
:last-child The last child element. li:last-child returns the last item
of each list.
:only-child Returns all elements that have no siblings.
:nth-child(n) The nth child element. li:nth-child(2) returns the second list item of each list.
:nth-child(even|odd) Even or odd children. li:nth-child(even) returns the even children of each list.
:nth-child(Xn+Y) The nth child element computed by the supplied formula. If Y is 0, it may be omitted. li:nth-child(3n) returns every third item, whereas
li:nth-child(5n+1) returns the item after every fifth element.
:even and :odd Even and odd matching elements page-wide. li:even returns every even
list item.
:eq(n) The nth matching element.
:gt(n) Matching elements after (and excluding) the nth matching element.
:lt(n) Matching elements before (and excluding) the nth matching element.
1.4 The jQuery custom filter selectors that give immense power to identify target elements
:animated Selects elements that are currently under animated control. Chapter 5 will cover animations and effects.
:button Selects any button (input[type=submit], input[type=reset],
input[type=button], or button).
:checkbox Selects only check box elements (input[type=checkbox]).
:checked Selects only check boxes or radio buttons that are checked (supported by CSS).
:contains(foo) Selects only elements containing the text foo.
:disabled Selects only form elements that are disabled in the interface (supported by CSS).
:enabled Selects only form elements that are enabled in the interface (supported by CSS).
:file Selects all file elements (input[type=file]).
:header Selects only elements that are headers; for example: <h1> through
<h6> elements.
:hidden Selects only elements that are hidden.
:image Selects form images (input[type=image]).
:input Selects only form elements (input, select, textarea, button).
:not(filter) Negates the specified filter.
:parent Selects only elements that have children (including text), but not empty elements.
:password Selects only password elements (input[type=password]).
:radio Selects only radio elements (input[type=radio]).
:reset Selects reset buttons (input[type=reset] or button[type=reset]).
:selected Selects option elements that are selected.
:submit Selects submit buttons (button[type=submit] or input[type=submit]).
:text Selects only text elements (input[type=text]).
:visible Selects only elements that are visible.
2. 操作 selector set
2.1 size
size()
Returns the count of elements in the wrapped set
Parameters
none
Returns
The element count
2.2 get
get(index)
Obtains one or all of the matched elements in the wrapped set. If no parameter is specified,all elements in the wrapped set are returned in a JavaScript array. If an index parameter is provided, the indexed element is returned.
Parameters
index (Number) The index of the single element to return. If omitted, the entire set is returned in an array.
Returns
A DOM element or an array of DOM elements.
2.3 index
index(element)
Finds the passed element in the wrapped set and returns its ordinal index within the set. If the element isn’t resident in the set, the value -1 is returned.
Parameters
element (Element) A reference to the element whose ordinal value is to be determined.
Returns
The ordinal value of the passed element within the wrapped set or -1 if not found.
2.4 add
add(expression)
Adds elements, specified by the expression parameter, to the wrapped set. The expression can be a selector, an HTML fragment, a DOM element, or an array of DOM elements.
Parameters
expression (String|Element|Array) Specifies what is to be added to the matched set.This parameter can be a jQuery selector, in which case any matched
elements are added to the set. If an HTML fragment, the appropriate elements are created and added to the set. If a DOM element or an array of DOM elements, they are added to the set.
Returns
The wrapped set.
2.5 not
not(expression)
Removes elements from the matched set according to the value of the expression parameter.If the parameter is a jQuery filter selector, any matching elements are removed. If an element reference is passed, that element is removed from the set.
Parameters
expression (String|Element|Array) A jQuery filter expression, element reference, or array of element references defining what is to be removed from the
wrapped set.
Returns
The wrapped set.
2.6 filter
filter(expression)
Filters out elements from the wrapped set using a passed selector expression, or a filtering function.
Parameters
expression (String|Function) Specifies a jQuery selector used to remove all elements that do not match from the wrapped set, or a function that makes the
filtering decision. This function is invoked for each element in the set, with the current element set as the function context for that invocation. Any element that returns an invocation of false is removed from the set.
Returns
The wrapped set.
2.7 slice
slice(begin,end)
Creates and returns a new wrapped set containing a contiguous portion of the matched set.
Parameters
begin (Number) The zero-based position of the first element to be included in the returned slice. end (Number) The optional zero-based index of the first element not to be included in the returned slice, or one position beyond the last element to be included. If omitted, the slice extends to the end of the set.
Returns
The newly created wrapped set.
2.8 Methods to obtain new wrapped set based on relationships
children() Returns a wrapped set consisting of all unique children of the wrapped elements.
contents() Returns a wrapped set of the contents of the elements, which may include text nodes, in the wrapped set. (Frequently used to obtain the contents of <iframe> elements.)
next() Returns a wrapped set consisting of all unique next siblings of the wrapped elements.
nextAll() Returns a wrapped set containing all the following siblings of the wrapped elements.
parent() Returns a wrapped set consisting of the unique direct parents of all wrapped elements.
parents() Returns a wrapped set consisting of the unique ancestors of all wrapped elements. This includes the direct parents as well as the remaining ancestors all the way up to, but not including, the document root.
prev() Returns a wrapped set consisting of all unique previous siblings of the wrapped elements.
prevAll() Returns a wrapped set containing all the previous siblings of the wrapped elements.
siblings() Returns a wrapped set consisting of all unique siblings of the wrapped elements.
2.9 find
find(selector)
Returns a new wrapped set containing all elements of the original set that match the passed selector expression.
Parameters
selector (String) A jQuery selector that elements must match to become part of the returned set.
Returns
The newly created wrapped set.
2.10 contains
contains(text)
Returns a new wrapped set composed of elements that contain the text string passed as the text parameter
Parameters
text (String) The text that an element must contain in order to be added to the returned set
Returns
The newly created wrapped set
2.11 is
is(selector)
Determines if any element in the wrapped set matches the passed selector expression
Parameters
selector (String) The selector expression to test against the elements of the wrapped set
Returns
true if at least one element matches the passed selector; false if not
2.12 end
end()
Used within a chain of jQuery command to back up the wrapped set to a previously returned set
Parameters
none
Returns
The previous wrapped set
2.13 andSelf
andSelf()
Merges the two previous wrapped sets in a command chain
Parameters
none
Returns
The merged wrapped set
来自:Manning.jQuery.in.Action.Feb.2008.pdf
相关阅读 更多 +