2011-05-31 64 views
1

有以下腳本:可以統計div ID內特定div類的數量嗎?

var CurrentPageTop = 0; 
    var PageHeight = 496; 
    var TotalPages = 9; 
    var CurrenPage = 1; 
    function Slide(control, panel){ 



      if (control.name == "SlideDown"){ 

        if ((CurrenPage >= 1) && (CurrenPage <= TotalPages-1)){ 
          CurrenPage++; 
          CurrentPageTop = (CurrentPageTop - PageHeight); 
        } 
      } 

      if (control.name == "SlideUp"){ 

        if ((CurrenPage > 1) && (CurrenPage <= TotalPages)){ 
          CurrenPage--; 
          CurrentPageTop = (CurrentPageTop + PageHeight); 
        } 
      }  

      var PanelScroller = document.getElementById(panel); 
      PanelScroller.style.top = CurrentPageTop + 'px'; 

      var CurrenPageId = document.getElementById("CurrenPageId"); 
      CurrenPageId.innerHTML = "Page " + CurrenPage + " of 9"; 
    } 

伴隨在頁面上一個圖像畫廊:

 <table cellpadding="0" cellspacing="0"> 
    <tr> 
      <td> 
        <div class="SlidingPanel"> 
          <div id="PanelScroller"> 
            <div class="Page"> 
     <a href="surf/1.jpg" rel="shadowbox[Surf]"><img src="i/gal/g1.jpg"></a> 
      </div> 

我將如何設置這有一個頁面上的多個實例?

一些畫廊有9張,有的有6等

有沒有一種方法來計算的<div class="Page">一個<div id="PanelScroller">中的數字?

我想避免對3個不同的畫廊使用3個略有不同的腳本實例。因爲我是新手,所以我無法把頭圍繞在它的周圍。

回答

0
function number_of_elements_with_class_name(class_name) { 
    var elements = getElementsByClassName(class_name); 
    var count = 0; 
    for(var i in elements) { 
     count++; 
    } 
    return count; 
}