<html>
<body>
  <div id="div1">
    
  </div>
  <script type="text/javascript">
    function FormatDiv(curPage)
    {
        var html = "";
        if(curPage<5)  //如果点击的页数小于5,就实现 1 2 3 4 5
        {
            for(var i=1;i<=5;i++)
            {
                html += '<span onclick=FormatDiv(this.innerText)>'+i+"</span> ";
            }
        }
        else  //如果点击的页数大于5,就显示 curPage-2 curPage-1 curPage curPage+1 curPage+2
        {
            for(var j=(parseInt(curPage)-2);j<=(parseInt(curPage)+2);j++)
            {
                html += '<span onclick=FormatDiv(this.innerText)>'+j+"</span> ";
            }
        }
        alert(html)
        document.getElementById('div1').innerHTML = html;
    }
    window.onload = function(){
        FormatDiv(1);  //初始选择的是第一页
    }
  </script>
</body>
</html>

解决方案 »

  1.   

    for(var j=parseInt(curPage)-2;j<=parseInt(curPage)+2;j++)
      

  2.   

    <html>
    <body>
      <div id="div1">
        
      </div>
      <script type="text/javascript">
        function FormatDiv(curPage)
        {
            var html = "";
            if(curPage<5)  //如果点击的页数小于5,就实现 1 2 3 4 5
            {
                for(var i=1;i<=5;i++)
                {
                    html += '<span onclick=FormatDiv('+j+')>'+i+"</span>";
                }
            }
            else  //如果点击的页数大于5,就显示 curPage-2 curPage-1 curPage curPage+1 curPage+2
            {
                for(var j=(curPage-2);j<=(curPage+2);j++)
                {
                    html += '<span onclick=FormatDiv('+j+')>'+j+"</span>";
                }
            }
            document.getElementById('div1').innerHTML = html;
        }
        window.onload = function(){
            FormatDiv(1);  //初始选择的是第一页
        }
      </script>
    </body>
    </html>
      

  3.   

    <html>
    <body>
      <div id="div1">
        
      </div>
      <script type="text/javascript">
        function FormatDiv(curPage)
        {
            var html = "";
            if(curPage<5)  //如果点击的页数小于5,就实现 1 2 3 4 5
            {
                for(var i=1;i<=5;i++)
                {
                    html += '<span onclick=FormatDiv('+i+')>'+i+"</span>";
                }
            }
            else  //如果点击的页数大于5,就显示 curPage-2 curPage-1 curPage curPage+1 curPage+2
            {
                for(var j=(curPage-2);j<=(curPage+2);j++)
                {
                    html += '<span onclick=FormatDiv('+j+')>'+j+"</span>";
                }
            }
            document.getElementById('div1').innerHTML = html;
        }
        window.onload = function(){
            FormatDiv(1);  //初始选择的是第一页
        }
      </script>
    </body>
    </html>
      

  4.   


    <html>
    <body>
      <div id="div1">
        
      </div>
      <script type="text/javascript">
        function FormatDiv(curPage)
        {
         curPage = parseInt(curPage);
            var html = "";
            if(curPage<=5) //如果点击的页数小于5,就实现 1 2 3 4 5
            {
                for(var i=1;i<=5;i++)
                {
                    html += '<span onclick=FormatDiv(this.innerText)>'+i+"</span>";
                }
            }
            else  //如果点击的页数大于5,就显示 curPage-2 curPage-1 curPage curPage+1 curPage+2
            {
                for(var j=curPage-2;j<=curPage+2;j++)
                {
                    html += '<span onclick=FormatDiv(this.innerText)>'+j+"</span>";
                }
            }
            document.getElementById('div1').innerHTML = html;
        }
        window.onload = function(){
            FormatDiv(7);  //初始选择的是第一页
        }
      </script>
    </body>
    </html>