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
- <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "JqeryClick. aspx. cs" Inherits = "AJAXEnabledWebApplication1.JqeryClick" %>
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head runat = "server">
- <Title> No title page </title>
- <Mce: script src = "jquery-1.3.2.min.js" mce_src = "jquery-1.3.2.min.js" type = "text/javascript"> </mce: script>
- <Mce: script type = "text/javascript"> <! --
- $ (Function (){
- $ ("# Btn"). click (function (){
- // Display the selected content in the ViewDiv drop-down list. The value is displayed.
- $ ("# ViewDiv" ).html ($ ("# ddlSel"). val () + "-----" + $ ("# Select1"). val ());
- // 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
- // So here I add a large div to retrieve what we want.
- // Filter (fn) is used to filter out the items containing ol.
- $ ("# All"). children ("div"). filter (function (index ){
- Return $ ("ol", this). size () = 0;
- }). Each (function (index ){
- // The content of these divs is displayed here. Please note that we use
- // $ ("Div", this#.html ()
- Alert ($ ("#" + this. id + "" ).html ());
- });
- })
- })
- // --> </Mce: script>
- </Head>
- <Body>
- <Form id = "form1" runat = "server">
- <Div id = "all">
- <Div id = "one">
- <Asp: DropDownList ID = "ddlSel" runat = "server">
- <Asp: ListItem Value = "1"> Item 1 </asp: ListItem>
- <Asp: ListItem Value = "2"> Item 2 </asp: ListItem>
- </Asp: DropDownList>
- <Select id = "Select1">
- <Option value = "3"> Item 3 </option>
- <Option value = "4"> Item 4 </option>
- </Select>
- <Input id = "btn" type = "button" value = ""/> </div>
- <Div id = "ViewDiv"> </div>
- <Div id = "2"> <ol> <li> Hello </li> </ol> </div> <div id = "1"> How are you? </Div>
- </Div>
- </Form>
- </Body>
- </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...