`

Managing the wrapped element set

阅读更多

Remember, any jQuery wrapped set can be used as an array of element references.

 

1.Determining the size of the wrapped set

Command syntax:size()

Returns the count of elements in the wrapped set

Parameters

none

Returns

The element count

 

2.Obtaining elements from the wrapped set

 

Command syntax: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.

 

 

3.Slicing and dicing the wrapped element set

 

Command syntax: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.

 

eg: $('p').add('<div>Hi there!</div>') //This fragment creates a wrapped set of all <p> elements in the document, and then creates a new <div>, and adds it to the wrapped set

 

 

Command syntax: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.

eg: $('img[title]').not('[title*=puppy]') //selects all the images with title attribute, then remove the element from the wrapped set which element's title contains "puppy"

 

 

Command syntax: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.

eg: $('td').filter(function(){return this.innerHTML.match(/^\d+$/)}) //This jQuery expression creates a wrapped set of all  <td> elements and then invokes the function passed to the filter() method for each, with the current matched elements as the this value for the invocation. The function uses a regular expression to determine if the element content matches the described pattern 

(a sequence of one or more digits), returning false if not. Every element whose filter function invocation returns false is removed from the wrapped set. 

 

 

Command syntax: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.

 

eg: $('*').slice(0,4);  //selects all elements on the page and then creates a set containing the first four elements.

 

4.Getting wrapped sets using relationships

Methods to obtain new wrapped set based on relationships

 

All of the methods above, with the exception of contents(), accept a parameter containing a string that can be used to filter the results.

 

 

5.Even more ways to use a wrapped set

 

Command syntax: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.

 

 

Command syntax: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

 

 

 

Command syntax: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

 

 

6.Managing jQuery chains

 

Command syntax: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

eg: $('img').clone().appendTo('#somewhere').end().addClass('beenCloned');  // clone the original set to the element which id is "somewhere", then add css class to the left of original set

 

 

Command syntax:andSelf()

Merges the two previous wrapped sets in a command chain

Parameters

none

Returns

The merged wrapped set

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics