javascript實現瀏覽器端列表分頁樣本

來源:互聯網
上載者:User

<body> 
<!--一塊用來顯示列表的地區--> 
<div id="comment"> </div> 
 
<!--一塊用來放置頁碼的地區--> 
<div id="pager" > </div> 
 
<script language="javascript" > 
/*全域數組,儲存評論內容*/ 
var arrList = new Array(); 
/*標記當前頁碼*/ 
var nCurPage = 0; 
/*每頁大小,一個常量*/ 
var PAGE_SIZE = 5; 
 
/*一個工具函數,可以很方便的根據id得到標籤對象*/ 
function $(id) 
{ 
    return document.getElementById(id); 
} 
 
/**
* 添加一個評論到全域列表
**/ 
function addCmt(comment) 
{ 
    arrList.push(comment); 
} 
 
/**
* 根據當前位置和每頁大小來重新整理顯示
**/ 
function display() 
{ 
    totalPage = arrList.length/PAGE_SIZE; 
     
    /*顯示當前頁的內容*/ 
    $("comment").innerHTML = "";// 先清空 
    for (c = PAGE_SIZE * nCurPage; c < arrList.length && c < PAGE_SIZE *(nCurPage+1); c++) 
        $("comment").innerHTML += "Page " + nCurPage + ": " + arrList[c] + "<br/>"; 
 
    /*顯示分頁欄目*/ 
    $("pager").innerHTML = "";// 先清空         
    for (i = 0; i < totalPage; i ++) 
        $("pager").innerHTML += "<a href='javascript:OnPageChanged(" + i + ")'>" + i + "</a>   "; 
} 
 
/**
* 使用者點擊page欄的數字
* 參數: pageset = 使用者點擊的頁碼
**/ 
function OnPageChanged(pageset) 
{ 
    nCurPage = pageset; 
    display(); 
} 
 
/**
* 使用者點擊添加評論按鈕
* 從文字框裡面取出內容,添加到全域列表,
* 然後重新整理一些顯示
*/ 
function OnAddComment() 
{ 
    addCmt($("cmt").value); 
    display(); 
} 
 
</script> 
<input type="text" name="cmt" value="hello"/> 
<input type="button" value="添加" onclick="OnAddComment();"/> 
</body> 

 


摘自 開心樂源的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.