2012-01-13 59 views
1

我正在學習jQuery,在閱讀書籍時,有一個代碼,您正在製作自定義選擇器。下面是代碼jQuery自定義選擇器代碼

(function($) { 
    $.extend($.expr[':'], { 
     group: function(element, index, matches, set) { 
      var num = parseInt(matches[3], 10); 
      if (isNaN(num)) { 
       return false; 
      } 
      return index % (num * 2) < num; 
     } 
    }); 
})(jQuery); 

,並在這裏呼籲

$(document).ready(function() { 
    function stripe() { 
     $('#news').find('tr.alt').removeClass('alt'); 
     $('#news tbody').each(function() { 
      $(this).children(':visible').has('td') 
       .filter(':group(3)').addClass('alt'); 
     }); 
    } 

    stripe(); 

}); 

我知道.filter()運行對每個子元素。假設如果我的tbody元素有4個tr(行),那麼對每個tr運行.filter(隱式迭代)。 現在,當調用.filter(':group(3)')時,我注意到傳遞給:group函數的參數是。

首次成爲

組:功能(元件,索引,火柴,設置)

元件是TR索引是0符合項目成爲[「:組(3)「,」組「,」「,」3「]組變成[tr,tr,tr,tr,tr]

現在,我設置了一組,每個tbody標籤都有tr的數量,這成爲了set數組。但是請告訴我,parseInt(匹配[3],10)如何工作?

我想問問jQuery如何製作匹配數組。我只寫:組(3)。如何將匹配值設置爲參數列表中的這些值?

這裏是HTML的代碼片段

<table id="news"> 
    <thead> 
     <tr> 
      <th>Date</th> 
      <th>Headline</th> 
      <th>Author</th> 
      <th>Topic</th> 
     </tr> 
    </thead> 

    <tbody> 
     <tr> 
      <th colspan="4">2011</th> 
     </tr> 
     <tr> 
      <td>Apr 15</td> 
      <td>jQuery 1.6 Beta 1 Released</td> 
      <td>John Resig</td> 
      <td>Releases</td> 
     </tr> 
     <tr> 
      <td>Feb 24</td> 
      <td>jQuery Conference 2011: San Francisco Bay Area</td> 
      <td>Ralph Whitbeck</td> 
      <td>Conferences</td> 
     </tr> 
     <tr> 
      <td>Feb 7</td> 
      <td>New Releases, Videos &amp; a Sneak Peek at the jQuery UI Grid</td> 
      <td>Addy Osmani</td> 
      <td>Plugins</td> 
     </tr> 
     <tr> 
      <td>Jan 31</td> 
      <td id="release">jQuery 1.5 Released</td> 
      <td>John Resig</td> 
      <td>Releases</td> 
     </tr> 
     <tr> 
      <td>Jan 30</td> 
      <td>API Documentation Changes</td> 
      <td>Karl Swedberg</td> 
      <td>Documentation</td> 
     </tr> 
    </tbody> 

    <tbody> 
     <tr> 
      <th colspan="4">2010</th> 
     </tr> 
     <tr> 
      <td>Nov 23</td> 
      <td>Team Spotlight: The jQuery Bug Triage Team</td> 
      <td>Paul Irish</td> 
      <td>Community</td> 
     </tr> 
     <tr> 
      <td>Oct 4</td> 
      <td>New Official jQuery Plugins Provide Templating, Data Linking and Globalization</td> 
      <td>John Resig</td> 
      <td>Plugins</td> 
     </tr> 
     <tr> 
      <td>Sep 4</td> 
      <td>The Official jQuery Podcast Has a New Home</td> 
      <td>Ralph Whitbeck</td> 
      <td>Documentation</td> 
     </tr> 
     <tr> 
      <td>Aug 24</td> 
      <td>jQuery Conference 2010: Boston</td> 
      <td>Ralph Whitbeck</td> 
      <td>Conferences</td> 
     </tr> 
     <tr> 
      <td>Aug 13</td> 
      <td>The jQuery Project is Proud to Announce the jQuery Mobile Project</td> 
      <td>Ralph Whitbeck</td> 
      <td>Plugins</td> 
     </tr> 
     <tr> 
      <td>Jun 14</td> 
      <td>Seattle jQuery Open Space and Hack Attack with John Resig</td> 
      <td>Rey Bango</td> 
      <td>Conferences</td> 
     </tr> 
     <tr> 
      <td>Mar 16</td> 
      <td>Microsoft to Expand its Collaboration with the jQuery Community</td> 
      <td>John Resig</td> 
      <td>Miscellaneous</td> 
     </tr> 
     <tr> 
      <td>Mar 15</td> 
      <td>jQuery Conference 2010: San Francisco Bay Area</td> 
      <td>Mike Hostetler</td> 
      <td>Conferences</td> 
     </tr> 
     <tr> 
      <td>Jan 14</td> 
      <td>jQuery 1.4 Released</td> 
      <td>John Resig</td> 
      <td>Releases</td> 
     </tr> 
     <tr> 
      <td>Jan 8</td> 
      <td>14 Days of jQuery and the New API Browser</td> 
      <td>John Resig</td> 
      <td>Documentation</td> 
     </tr> 
    </tbody> 

    <tbody> 
     <tr> 
      <th colspan="4">2005</th> 
     </tr> 
     <tr> 
      <td>Dec 17</td> 
      <td>JSON and RSS</td> 
      <td>John Resig</td> 
      <td>Documentation</td> 
     </tr> 
     <tr> 
      <td>Dec 14</td> 
      <td>Google Homepage API</td> 
      <td>John Resig</td> 
      <td>Miscellaneous</td> 
     </tr> 
     <tr> 
      <td>Dec 13</td> 
      <td>XPath and CSS Selectors</td> 
      <td>John Resig</td> 
      <td>Miscellaneous</td> 
     </tr> 
     <tr> 
      <td>Dec 12</td> 
      <td>Sparklines with JavaScript and Canvas</td> 
      <td>John Resig</td> 
      <td>Miscellaneous</td> 
     </tr> 
    </tbody> 

</table> 

感謝

回答

1

在你的榜樣,matches[3] == "3"。因此parseInt(matches[3], 10);會將「3」轉換爲整數3(基數10 =十進制)。

+0

hhmm好的謝謝:)以及如何創建數組** [「:group(3)」,「group」,「」,「3」] **? – Basit 2012-01-13 09:15:33

+0

,這取決於它是如何實現的。我認爲這將是用()分組括號的某種正則表達式。每個正則表達式的組將是該數組的一個條目。也許別人可以對此有所瞭解。 – devnull69 2012-01-13 09:43:54

+0

我找到了這個解釋,但我不明白它在說什麼**匹配:一個包含正則表達式結果的數組,用於解析這個選擇器。通常,匹配[3]是數組中唯一相關的項目;在形式選擇器中:a(b),匹配項[3]包含b,括號內的文本**。你能解釋給我嗎?謝謝 – Basit 2012-01-13 10:29:15