Juqery learning-4 filtering-jquery

Source: Internet
Author: User
Juqery (filter function description). For more information about jquery, see. Eq (index) obtains the position of the nth element from 0. Reduce the set of matched elements to a single element. The position of the element in the set of matched elements starts at 0 and goes to length-1. Return Value

JQuery

Parameters

Index(Integer): Index of an element in a jQuery object

Example

Obtain the Second Matching Element.

HTML code:

This is just a test.

So is this

JQuery code:

$ ("P"). eq (1)

Result:

[

So is this

]
Bytes --------------------------------------------------------------------------------------------------------------
HasClass (class) checks whether the current element contains a specific class. If yes, return true. This is actually is ("." + class ). Checks the current selection against a class and returns true, if at least one element of the selection has the given class. This is an alternative to is ("." + class). Return Value

Boolean

Parameters

Class(String): the name of the matched class.

Example

Animation of an element containing a class.

HTML code:

JQuery code:

$ ("P"). click (function (){
If ($ (this). hasClass ("protected "))
$ (This)
. Animate ({left:-10 })
. Animate ({left: 10 })
. Animate ({left:-10 })
. Animate ({left: 10 })
. Animate ({left: 0 });
});
Bytes --------------------------------------------------------------------------------------------------------------
Filter (expr) filters out the set of elements that match the specified expression. This method is used to narrow the matching range. Multiple Expressions separated by commas Removes all elements from the set of matched elements that do not match the specified expression (s ). this method is used to narrow down the results of a search. provide a comma-separated list of expressions to apply multiple filters at once. return Value

JQuery

Parameters

Expr(Expression): Expression

Example

Retain elements with select classes

HTML code:

Hello

Hello Again

And Again

JQuery code:

$ ("P"). filter (". selected ")

Result:

[

And Again

]

Retain the first element and the element with the select class

HTML code:

Hello

Hello Again

And Again

JQuery code:

$ ("P"). filter (". selected,: first ")

Result:

[

Hello

,

And Again

]
Bytes --------------------------------------------------------------------------------------------------------------
Filter (fn) filters out the element set that matches the returned value of the specified function. This function calculates each object once (as '$. each '). if the called function returns false, the element is deleted; otherwise, the element is retained. Removes all elements from the set of matched elements that does not match the specified function. the function is called with a context equal to the current element (just like '$. each '). if the function returns false, then the element is removed-anything else and the element is kept. return Value

JQuery

Parameters

Fn(Function): The Function passed into the filter.

Example

The reserved sub-element does not contain ol elements.

HTML code:

  1. Hello

How are you?

JQuery code:

$ ("P"). filter (function (index ){
Return $ ("ol", this). length = 0;
});

Result:

[

How are you?

]
Bytes --------------------------------------------------------------------------------------------------------------
Is (expr) uses an expression to check the selected element set. If at least one element matches the given expression, true is returned. If no element matches, or the expression is invalid, 'false'. 'filter' is actually called internally. Therefore, the original rules of the filter () function are also applicable here. Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression. if no element fits, or the expression is not valid, then the response will be 'false '. 'filter' is used internally, therefore all rules that apply there apply here, as well. return Value

Boolean

Parameters

Expr(String): the filter expression.

Example

Returns true because the parent element of an input element is a form element.

HTML code:

JQuery code:

$ ("Input [type = 'checkbox']"). parent (). is ("form ")

Result:

True
Bytes --------------------------------------------------------------------------------------------------------------
Map (callback) converts a group of elements into other arrays (whether it is an array of elements or not). You can use this function to create a list, whether it is a value, attribute, or CSS style, or other special forms. You can use '$. map ()' to create a map. Translate a set of elements into another set of values (which may, or may not, be elements ). you cocould use this to build lists of values, attributes, css values-or even perform special, custom, selector transformations. this is provided as a convenience method for using '$. map ()'. return Value

JQuery

Parameters

Callback(Function): The Function executed for each element.

Example

Create a list of values of each input element in form.

HTML code:

Values:


JQuery code:

$ ("P"). append ($ ("input"). map (function (){
Return $ (this). val ();
}). Get (). join (","));

Result:

[

John, password, http://ejohn.org/

]
Bytes --------------------------------------------------------------------------------------------------------------
Not (expr) deletes elements that match the specified expression Removes elements matching the specified expression from the set of matched elements. Return Value

JQuery

Parameters

Expr(String, DOMElement, Array ): An expression, an element, or a group of elements.

Example

Delete an element with a select ID from element p

HTML code:

Hello

Hello Again

JQuery code:

$ ("P"). not ($ ("# selected") [0])

Result:

[

Hello

] Bytes
Slice (start, [end]) selects a matched subset similar to the original slice Method Selects a subset of the matched elements. Behaves exactly like the built-in Array slice method. Return Value

JQuery

Parameters

Start(Integer): Start to select the position of the subset. The first element is 0. If it is a negative number, it can be selected from the end of the set.

End(Integer): (optional) end to select its position. If not specified, it is the end of itself.

Example

Select the first p element.

HTML code:

Hello

Cruel

World

JQuery code:

$ ("P"). slice (0, 1). wrapInner (" ");

Result:

[

Hello

]

Select the first two p elements

HTML code:

Hello

Cruel

World

JQuery code:

$ ("P"). slice (0, 2). wrapInner (" ");

Result:

[

Hello

,

Cruel

]

Select only the second p element

HTML code:

Hello

Cruel

World

JQuery code:

$ ("P"). slice (1, 2). wrapInner (" ");

Result:

[

Cruel

]

Select only the second and third p elements.

HTML code:

Hello

Cruel

World

JQuery code:

$ ("P"). slice (1). wrapInner (" ");

Result:

[

Cruel

,

World

]

Selects all paragraphs, then slices the selection to include only the third element.

HTML code:

Hello

Cruel

World

JQuery code:

$ ("P"). slice (-1). wrapInner (" ");

Result:

[

World

]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.