Detailed descriptions of node operation methods such as each (), find (), and filter () in jQuery (recommended)

Source: Internet
Author: User
This article mainly introduces information about node operation methods such as each (), find (), and filter () in jQuery. It is very helpful and has reference value. If you need it, refer to section 1. each (callback)

Official explanation:

Returned value: jQuery

Overview

Execute a function using each matching element as the context.

This means that each time the passed function is executed, the this keyword in the function points to a different DOM element (each time it is a different matching element ). In addition, each time a function is executed, a numeric value indicating the position of the element used as the execution environment in the matched element set is passed to the function as the parameter (an integer starting from scratch ). If 'false' is returned, the loop is stopped (just like using 'Break' in a normal loop '). Return 'true' to jump to the next loop (just like using 'contine' in a normal loop ').

Parameters

Callback

The function to be executed for each Matching Element

Example

Description:

Iterate two images and set their src attributes. Note: this indicates a DOM object instead of a jQuery object.

HTML code:

JQuery code:

$("img").each(function(i){this.src = "test" + i + ".jpg";});

Result:

[ ,  ]

2. find (expr | obj | ele)

Official explanation:

Returned value: jQuery

Overview

Searches for all elements that match the specified expression. This function is a good way to find the child element of the element being processed.

All searches rely on jQuery expressions. This expression can be written using the selector syntax of the CSS1-3.

Parameters

Expr String
Expression used for searching
JQuery object
A jQuery object used to match Elements
Element DOMElement
A dom Element

Example

Description:

Search for the following span element from all paragraphs. Same as $ ("p span.

HTML code:

Hello, how are you?

JQuery code:

$("p").find("span")

Result:

[Hello]

3. filter (expr | obj | ele | fn)

Official explanation:

Overview

Filters out a set of elements that match the specified expression.

This method is used to narrow the matching range. Multiple Expressions separated by commas

Parameters

Expr String
String value, which contains the selector expression used to match the current element set.
JQuery object
Existing jQuery object to match the current element.
Element Expression
A dom element used to match elements.
Function (index) Function
A function is used as a set of test elements. It accepts an index parameter, which is the index of the element in the jQuery set. In a function, this indicates the current DOM element.

Example

Description of the selector parameter:

Retain elements with select classes

HTML code:

Hello

Hello Again

And Again

JQuery code:

$("p").filter(".selected")

Result:

[

And Again

]

The above section describes the detailed (recommended) knowledge of node operation methods such as each (), find (), and filter () in jQuery, which I hope to help you, if you have any questions, please leave a message and the editor will reply to you in time. I would like to thank you for your support for PHP chinnet!


For more information about node operation methods such as each (), find (), and filter () in jQuery (recommended), please follow the PHP Chinese network!

Related Article

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.