可以计算div id中特定div类的数量吗?

时间:2011-05-31 17:38:32

标签: javascript

拥有以下脚本:

    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个略有不同的此脚本实例。我无法绕过它,因为我是新手。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分: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;
}