2012-03-01 71 views
0

我一直在努力工作一段時間,使此代碼完美工作。 所以我所做的一切工作,除了 恐怕我必須張貼整個代碼在這裏的分頁按鈕好:jquery分頁代碼中的小問題

$(document).ready(function() { 

var pager = new Pager('comments', 3); 
     pager.init(); 
     pager.showPageNav('Pager', 'pageNavPosition'); 
     pager.showPage(3); 



function Pager(class_name, itemsPerPage) { 
    this.class_name = class_name; 
    this.itemsPerPage = itemsPerPage; 
    this.currentPage = 1; 
    this.pages = 0; 
    this.inited = false; 

    this.showRecords = function(from, to) {   
     var rows = $('.' + class_name); 
     // i starts from 1 to skip table header row 
     for (var i = 0; i < rows.length; i++) { 
      if (i < from || i > to) 
       rows[i].style.display = 'none'; 
      else 
       rows[i].style.display = ''; 
     } 
    } 

    this.showPage = function(pageNumber) { 
     if (! this.inited) { 
      alert("not inited"); 
      return; 
     } 

     var oldPageAnchor = document.getElementById('pg'+this.currentPage); 
     oldPageAnchor.className = 'pg-normal'; 

     this.currentPage = pageNumber; 
     var newPageAnchor = document.getElementById('pg'+this.currentPage); 
     newPageAnchor.className = 'pg-selected'; 

     var from = (pageNumber - 1) * itemsPerPage; 
     var to = from + itemsPerPage - 1; 
     this.showRecords(from, to); 
    } 

    this.prev = function() { 
     if (this.currentPage > 1) 
      this.showPage(this.currentPage - 1); 
    } 

    this.next = function() { 
     if (this.currentPage < this.pages) { 
      this.showPage(this.currentPage + 1); 
     } 
    }       

    this.init = function() { 
     var rows = $('.' + class_name); 
     var records = (rows.length); 
     this.pages = Math.ceil(records/itemsPerPage); 
     this.inited = true; 
    } 

    this.showPageNav = function(pagerName, positionId) { 
     if (! this.inited) { 
      alert("not inited"); 
      return; 
     } 

,我的問題是在這裏:

 var element = document.getElementById(positionId); 

     var pagerHtml = '<span onClick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | '; 
     for (var page = 1; page <= this.pages; page++) 
      pagerHtml += '<span id="pg' + page + '" class="pg-normal" onClick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | '; 
     pagerHtml += '<span onClick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>'; 

     element.innerHTML = pagerHtml; 
    } 
} 
          }); 

,如果你想看演示,請遵循波紋管 http://jsfiddle.net/J3Qnx/11/

回答

0

http://jsfiddle.net/justiceerolin/tes5N/

鏈接210

在$ .ready內創建尋呼機並不會在全局創建它,因此在$ .ready完成後(點擊等)將無法引用尋呼機

解決方案是創建全局尋呼機實例,thisPager,並引用該實例。

的變化是前四行:

window.thisPager= new Pager('comments', 3); 
thisPager.init(); 
thisPager.showPageNav('thisPager', 'pageNavPosition'); 
thisPager.showPage(3); 
+0

我愛你的男人:) – 2012-03-01 06:54:21

+0

也愛你的兄弟。如果這解決了問題,請將其標記爲已回答。感謝和好運! – 2012-03-01 06:56:45