最簡單的 DIV + Javascript 分頁示範代碼

來源:互聯網
上載者:User

手中有一個改進Web Part 的任務,要求加一個分頁,及對應前一頁,後一頁導航,以下為一個很簡單的驗證代碼:

<html>
<body>
<head>
<script Language = 'JavaScript'>

var curPage =0; maxPage =4;
function selectedPageByID(inputId)
{
 var divs = document.getElementsByTagName('div');
 var divCounter = 1;
 var divcountlength = divs.length;
 for(var no = 0; no < divcountlength; no++)
 {
  if(divs[no].id == inputId)
  {
   divs[no].style.display = "block";
   curPage = no;
  }
  else
  {
   divs[no].style.display = "none";
  }
 }
}

function selectedPageByNo(pageNo)
{
 var divs = document.getElementsByTagName('div');
 var divCounter = 1;
 var divcountlength = divs.length;
 for(var no = 0; no < divcountlength; no++)
 {
  if(no == pageNo)
  {
   divs[no].style.display = "block";
   curPage = no;
   
  }
  else
  {
   divs[no].style.display = "none";
  }
 }
}

function getNextPage()
{
 curPage = parseInt(curPage)+1;
 if (curPage > maxPage) curPage = maxPage;
 selectedPageByNo(curPage);
}

function getPrePage()
{
 curPage = parseInt(curPage)-1;
 if (curPage < 0) curPage = 0;
 selectedPageByNo(curPage);
}

</script>
</head>
<a href='javascript:selectedPageByNo(0);'> |< </a>
<a href='javascript:getPrePage();'> < </a>
<a href='javascript:selectedPageByID("Page1");'> 1 </a>
<a href='javascript:selectedPageByID("Page2");'> 2 </a>
<a href='javascript:selectedPageByID("Page3");'> 3 </a>
<a href='javascript:selectedPageByID("Page4");'> 4 </a>
<a href='javascript:selectedPageByID("Page5");'> 5 </a>
<a href='javascript:getNextPage();'> > </a>
<a href='javascript:selectedPageByNo(4);'> >| </a>

<hr>

<div id ='Page1' style='display:none'>
Hello!<br>This is testing Page 1.
</div>
<div id = 'Page2' style='display:none'>
Hello!<br>This is testing Page 2.
</div>
<div id = 'Page3' style='display:none'>
Hello!<br>This is testing Page 3.
</div>
<div id = 'Page4' style='display:none'>
Hello!<br>This is testing Page 4.
</div>
<div id = 'Page5' style='display:none'>
Hello!<br>This is testing Page 5.
</div>

</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.