javascript實現json頁面分頁執行個體代碼

來源:互聯網
上載者:User

javascript實現json頁面分頁執行個體代碼

 這篇文章主要介紹了javascript實現json頁面分頁執行個體代碼,需要的朋友可以參考下

下午有個朋友問json 資料怎麼分頁 就搗鼓了一個東東出來 

 

下面直接代碼:

代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>無標題文檔</title>

<script>

var a={"code":1,"list":[{"category1_id":"1","category1_name":"u9152u6c34u996eu6599","category1_intro":"u6e05u51c9u53efu53e3

u7684u9152u6c34u996eu6599","category1_images":"http://www.songcuomen.com/attachments/pro

ductType/2014-01/10/21_1389343628.jpg"},{"category1_id":"2","category1_name":"u7c73u9762u7caeu6cb9","category1_intro":"u5bb6u5eadu5fc5u5907",

"category1_images":"http://www.songcuomen.com/attachments/productType/2014-01/10/21_1389343865.jpg"},{"category1_id":"3","category1_name":"u7f8eu5bb9u6d17u62a4","category1_intro":"u62a4u80a4u5fc5u5907","

category1_images":"http://www.songcuomen.com/attachments/productType/2014-01/10/21_1389343935.jpg"},{"category1_id":"4","category1_name":"u6e05u6d01u6d17u6da4","category1_intro":"u7ed9u60a8u4e00u4e2a

u5e72u51c0u7684u751fu6d3bu73afu5883","category1_images":"http://www.songcuomen.com/attachm

ents/productType/2014-01/10/21_1389343986.jpg"},{"category1_id":"5","category1_name":"u751fu6d3bu7528u54c1","category1_intro":"u5bb6u5eadu5fc5u5907","

category1_images":"http://www.songcuomen.com/attachments/productType/2014-01/10/21_1389344445.jpg"},{"category1_id":"6","category1_name":"u4f11u95f2u98dfu54c1","category1_intro":"u597du5403u7f8eu5473",

"category1_images":"http://www.songcuomen.com/attachments/productType/2014-01/10/21_1389344494.jpg"}]};

var y=1;//表示頁數

var t=2;//表示每頁現實幾條資料

var z=a["list"].length;

var zy= Math.ceil(z/t);

window.onload=function (){

fanye(0);

}

function fanye(f)

{

    if(y==1 && f==-1)

    {

        alert('已經是第一頁了');

        f=0;

        }

    if(y==zy && f==1)

    {

        alert('已經是最後一頁了');

        f=0;

        }    

 

     var otbod=document.getElementById('table').tBodies[0];

     var s=otbod.rows.length;

    for(i=0;i<s;i++)

    {

 

        otbod.removeChild(otbod.rows[0]);

    }

        y+=f;

       var q=(y-1)*2;//頁數-1 乘以每頁現實多少條 結果為 本頁從第幾條開始

       for(var i=1;i<3;i++)

    {

 

    var otr=document.createElement('tr');

    var otd1=document.createElement('td');

    var otd2=document.createElement('td');

    var otd3=document.createElement('td');

    var otd4=document.createElement('td');

    otd1.innerHTML=a["list"][q]['category1_id'];

    otd2.innerHTML=a["list"][q]['category1_name'];

    otd3.innerHTML=a["list"][q]['category1_intro'];

    otd4.innerHTML=a["list"][q]['category1_images'];

    otr.appendChild(otd1);

    otr.appendChild(otd2);

    otr.appendChild(otd3);

    otr.appendChild(otd4);

     otbod.appendChild(otr);

     if(q==zy)

     {

         break;

         }

    q++;

    }

}

</script>

 

</head>

 

<body>

 

<table  border="1" id="table">

<thead>

<tr><td>category1_id</td><td>category1_name</td><td>category1_intro</td><td>category1_images</td></tr>

</thead>

<tbody>

 

</tbody>

</table>

<a href="javascript:fanye(1);">下一頁</a>

<a href="javascript:fanye(-1);">上一頁</a>

</body>

</html>

聯繫我們

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