JS寫動態分頁樣式效果

來源:互聯網
上載者:User

標籤:nbsp   doc   images   css   asc   .class   display   傳回值   idt   

如下:

html:

        <body>        <div>            <table id="btnbox">                <tbody>                <tr><td>                    <a href="#" class="btn">上一頁</a>                    <a href="#" index="1">1</a>                    <a href="#" index="2">2</a>                    <a href="#" index="3">3</a>                    <a href="#" index="4">4</a>                    <a href="#" index="5">5</a>                    <a href="#" index="6">6</a>                    <a href="#" index="7">7</a>                    <a href="#" index="8">8</a>                    <a href="#" class="btn">下一頁</a>                </td></tr>                </tbody>            </table>        </div>    </body>

 

CSS:

        <style>            a{                                                  /* a標籤樣式 */                width:17px;                height:20px;                display: inline-block;                text-align: center;                text-decoration:none;                border: 1px #000000 solid;                font:12px/20px "宋體";            }            .btn{                                              /* 上一頁下一頁的寬 */                width:60px;            }            #btnbox .on{color:#ffffff;background:blue;}      /* 預先設定好被點擊的分頁按鈕的被點擊狀態 ,通過修改a標籤的classname進行動態修改當前選中狀態 */            a:hover{                                         /* hover 使滑鼠移上去顯示背景顏色及邊框 */                color:#ffffff;                background:blue;                }            table{                                          /* 在div中置中顯示 */                margin: 0 auto;            }        </style>

 

JS:

<script type="text/javascript" language="javascript">window.onload = function(){var btnlist = document.getElementById(‘btnbox‘).getElementsByTagName(‘a‘);         //擷取table下的所有a標籤   注意這裡的傳回值是一個偽數組,並不支援對數組進行splice等操作,相關偽數組資料自行瞭解。var index;                                                 //定義一個記錄當前選中按鈕的變數for(var i=0;i<btnlist.length;i++){                         //使用for迴圈對1-8按鈕進行點擊事件綁定    if(i!=0 && i<btnlist.length-1){                        //因為上一頁下一頁也在數組btnlist裡面,所以需要排除        btnlist[i].onclick = function(){            clearbtn();            this.className = ‘on‘;                           //當滑鼠點擊該按鈕時給按鈕設定選中樣式index = parseInt(this.getAttribute(‘index‘));               //取出當前按鈕即a標籤的自訂index值  使用 getAttrinbute 方法 需要轉換成整數進行計算        }    }}btnlist[0].onclick = function(){                           //為上一頁綁定點擊事件    if(index != 0){goto(‘-‘);}}btnlist[btnlist.length-1].onclick = function(){             //為下一頁綁定點擊事件    if(index != 0){goto(‘+‘);}                              }function goto(x){                                          //當點擊上一頁下一頁時進行的前後判斷操作    console.log(x);    if(x == ‘-‘ && index != 1){    index = index - 1;}else if(x == ‘+‘ && index != 8){    index = index + 1;}clearbtn();btnlist[index].className = ‘on‘;                            //設定當前選中按鈕樣式}function clearbtn(){                                        // 清除選中樣式    for(var i=1;i<(btnlist.length-1);i++){        if(btnlist[i].className == ‘on‘){btnlist[i].className = ‘‘;break;}            }        }        btnlist[1].onclick();    }</script>

 

JS寫動態分頁樣式效果

聯繫我們

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