jquery中filter(fn)的使用研究

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

jquery中filter(fn)給出的官方說明是:

 

篩選出與指定函數傳回值匹配的元素集合這個函數內部將對每個對象計算一次 (正如 ‘$.each‘). 如果調用的函數返回false則這個元素被刪除,否則就會保留。 這個說明沒有問題,可是給出的例子卻有問題。例子是

HTML 程式碼:

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

jQuery 代碼:

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

結果:

[ <p>How are you?</p> ] 可是大家在試的時候會發現,<p>中是不讓放<ol>的,在一些瀏覽器會報錯。 我在自己的一個程式中,用到了filter(fn)這個方法。我就把我使用的例子放出來。 我要做的功能其實很簡單,就是要把一個頁面中所有的<div>內容顯示出來。這裡面有些<div>的內容是動態載入的。  [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>無標題頁</title>  
  6.       
  7.       
  8.     <mce:script src="jquery-1.3.2.min.js" mce_src="jquery-1.3.2.min.js" type="text/javascript"></mce:script>  
  9.       
  10.       
  11.     <mce:script type="text/javascript"><!--  
  12.        $(function(){  
  13.           $("#btn").click(function(){  
  14.             
  15.             //在ViewDiv中顯示二種下拉式清單當前被選中的內容。顯示出來的是value值。  
  16.             $("#ViewDiv").html($("#ddlSel").val() + "-----" + $("#Select1").val());  
  17.               
  18.               
  19.             //這裡有一點我要說明的事,我使用的.aspx檔案,這種檔案會在<form>內自己產生自己的幾個<div>,它不是我要取的  
  20.             //所以在這裡我加了一個大的div來取我們所要的。  
  21.               
  22.             //這裡用filter(fn)來過濾掉裡麵包含ol的項  
  23.             $("#all").children("div").filter(function(index) {  
  24.                     return $("ol", this).size() == 0;  
  25.             }).each(function(index){  
  26.               
  27.             //這裡顯示出這些div的內容。請注意,在這裡我們用  
  28.             //$("div",this).html()這種方法  
  29.                alert($("#"+this.id+"").html());  
  30.             });   
  31.           })  
  32.        })  
  33.       
  34. // --></mce:script>  
  35. </head>  
  36. <body>  
  37.     <form id="form1" runat="server">  
  38.     <div id="all">  
  39.     <div id="one">  
  40.         <asp:DropDownList ID="ddlSel" runat="server">  
  41.             <asp:ListItem Value="1">第一項</asp:ListItem>  
  42.             <asp:ListItem Value="2">第二項</asp:ListItem>  
  43.         </asp:DropDownList>  
  44.         <select id="Select1">  
  45.             <option value="3">第三項</option>  
  46.             <option value="4">第四項</option>  
  47.         </select>  
  48.         <input id="btn" type="button" value="顯示資訊" /></div>  
  49.       
  50.     <div id="ViewDiv"></div>  
  51.       
  52.       
  53.       
  54.     <div id="2"><ol><li>Hello</li></ol></div><div id="1">How are you?</div>   
  55.       
  56.     </div>  
  57.     </form>  
  58. </body>  
  59. </html>  

jquery中filter(fn)的使用研究

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.