Research on the use of filter (fn) in jquery, jqueryfilter

Source: Internet
Author: User

Research on the use of filter (fn) in jquery, jqueryfilter

The official description of filter (fn) in jquery is as follows:

 

Filter 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. This statement is correct, but the example is incorrect. Example:

HTML code:

<P> <ol> <li> Hello </li> </ol> </p> <p> How are you? </P>

JQuery code:

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

Result:

[<P> How are you? </P>] However, when you try it, you will find that <p> is not allowed to <ol>, and errors will be reported in some browsers. In my own program, I used the filter (fn) method. I will release my example. The function I want to do is to display all the <div> content on a page. Some <div> content is dynamically loaded. [C-sharp]View plaincopy
  1. <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqeryClick. aspx. cs" Inherits = "AJAXEnabledWebApplication1.JqeryClick" %>
  2. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <Html xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head runat = "server">
  5. <Title> No title page </title>
  6. <Mce: script src = "jquery-1.3.2.min.js" mce_src = "jquery-1.3.2.min.js" type = "text/javascript"> </mce: script>
  7. <Mce: script type = "text/javascript"> <! --
  8. $ (Function (){
  9. $ ("# Btn"). click (function (){
  10. // Display the selected content in the ViewDiv drop-down list. The value is displayed.
  11. $ ("# ViewDiv" ).html ($ ("# ddlSel"). val () + "-----" + $ ("# Select1"). val ());
  12. // Here is something I want to explain. The. aspx file I use will generate several of my own <div> files in <form>, which is not what I want to obtain
  13. // So here I add a large div to retrieve what we want.
  14. // Filter (fn) is used to filter out the items containing ol.
  15. $ ("# All"). children ("div"). filter (function (index ){
  16. Return $ ("ol", this). size () = 0;
  17. }). Each (function (index ){
  18. // The content of these divs is displayed here. Please note that we use
  19. // $ ("Div", this#.html ()
  20. Alert ($ ("#" + this. id + "" ).html ());
  21. });
  22. })
  23. })
  24. // --> </Mce: script>
  25. </Head>
  26. <Body>
  27. <Form id = "form1" runat = "server">
  28. <Div id = "all">
  29. <Div id = "one">
  30. <Asp: DropDownList ID = "ddlSel" runat = "server">
  31. <Asp: ListItem Value = "1"> Item 1 </asp: ListItem>
  32. <Asp: ListItem Value = "2"> Item 2 </asp: ListItem>
  33. </Asp: DropDownList>
  34. <Select id = "Select1">
  35. <Option value = "3"> Item 3 </option>
  36. <Option value = "4"> Item 4 </option>
  37. </Select>
  38. <Input id = "btn" type = "button" value = ""/> </div>
  39. <Div id = "ViewDiv"> </div>
  40. <Div id = "2"> <ol> <li> Hello </li> </ol> </div> <div id = "1"> How are you? </Div>
  41. </Div>
  42. </Form>
  43. </Body>
  44. </Html>

What is filter () in jquery?

JQuery API definition:
1. jQueryfilter (fn)
Filters out a set of elements that match the specified function return value.
HTML code:
<P> <ol> <li> Hello </li> </ol> </p> <p> How are you? </P>
JQuery code:
$ ("P"). filter (function (index ){
Return $ ("ol", this). length = 0;
});
Result:
[<P> How are you? </P>]

2. filter (expr)
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
Example Description: retain the elements with the select class
HTML code:
<P> Hello </p> <p> Hello Again </p> <p class = "selected"> And Again </p>
JQuery code:
$ ("P"). filter (". selected ")
Result:
[<P class = "selected"> And Again </p>]

Filter Method in jquery

By... I am also confused ,., Why...

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.