Research on the use of filter (FN) in jquery

Source: Internet
Author: User

The official note given by the filter (FN) in jquery is:

Filters out the collection of elements that match the return value of the specified function the inside of the function computes each object once (as ' $.each '). If the called function returns false then this element is deleted, otherwise it will be preserved. There is no problem with this explanation, but the example given is problematic. Example is

HTML Code:

<p><ol><li>hello</li></ol></p><p>how is you?</p>

JQuery Code:

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

Results:

[<p>how is you?</p>] but everyone in the test will find the,<p> is not allowed to put <ol>, in some browsers will error. I used the filter (FN) method in one of my programs. I'll just put out the example I'm using. The function that I want to do is actually very simple, it is to display all the <div> content of a page. Some of the <div> content is dynamically loaded. [C-sharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
  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. "http://www.w3.org/1999/xhtml" >
  4. "Server" >
  5. <title> Untitled 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. two drop-down lists are currently selected in Viewdiv.  The value is displayed.
  11. $ ("#ViewDiv"). HTML ($ ("#ddlSel"). Val () + "-----" + $ ("#Select1"). Val ());
  12. //Here's a little bit of what I'm going to illustrate, I'm using an. aspx file that will generate itself within <form> a few <div>
  13. //So here I add a big div to fetch what we want.
  14. //Use Filter (FN) here to remove the items containing OL
  15. $ ("#all"). Children ("div"). Filter (function (index) {
  16. return $ ("ol", this ). Size () = = 0;
  17. }). Each (function (index) {
  18. //The contents of these div are shown here. Please note that here we use
  19. //$ ("div", this). HTML () This method
  20. Alert ($ ("#" +this.id+""). html ());
  21. });
  22. })
  23. })
  24. --></mce:script>
  25. <body>
  26. <form id="Form1" runat= "Server" >
  27. <div id="All" >
  28. <div id="one" >
  29. <asp:dropdownlist id="Ddlsel" runat= "Server" >
  30. <asp:listitem value="1" > First item </asp:ListItem>
  31. <asp:listitem value="2" > Second item </asp:ListItem>
  32. </asp:DropDownList>
  33. <select id="Select1" >
  34. <option value="3" > Third </option>
  35. <option value="4" > Fourth </option>
  36. </select>
  37. <input id="btn" type="button" value="show Information"/></div>
  38. <div id="Viewdiv" ></div>
  39. <div id="2" ><ol><li>hello</li></ol></div><div id="1" >How is You ?</div>
  40. </div>
  41. </form>
  42. </body>

Research on the use of filter (FN) in jquery

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.