长文章javascript分页(多浏览器兼容修正版)

来源:互联网 发布:小区业主号码采集软件 编辑:程序博客网 时间:2024/04/30 17:02

先废话两句:
网上流传的这个js分页原版有如下2个Bug:
1.把"上一页 下一页"钮换成图片点击超链接 <a href="#"><img src="图片文件"></a> 后不能弹到页首(这个我也没解决)
2.在 firefox 和  opera 里标签divContent不显示.

在 firefox里不兼容是因为 firefox 不支持 this 关键字(我试的时候是这样的,有不同意见的网友希望能给我发email)
在opera 里不兼容是因为 程序中使用了 goto, opera 里视 goto 为关键字顾不显示

下面是修改过的代码:

<div id="divContent" name="divContent"></div>
 <br>
 <div id="divPagenation" name="divPagenation" style="font-size:12px;" align="center"></div> 

<script>
s="文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容";

// 封装DHTMLpagenation
function DHTMLpagenation(content)
{
 with (DHTMLpagenation)
 {
  // client static html file pagenation
  // Scipit by blueDestiny,never-online, www.never-online.net

  DHTMLpagenation.content=content;     // 内容
  DHTMLpagenation.contentLength=content.length;   // 内容长度
  DHTMLpagenation.pageSizeCount;     // 总页数
  DHTMLpagenation.perpageLength=100;    //default perpage byte length.
  DHTMLpagenation.currentPage=1;     // 起始页为第1页
  DHTMLpagenation.regularExp=//d+/;     // 建立正则表达式,匹配数字型字符串。
  
  DHTMLpagenation.divDisplayContent;
  DHTMLpagenation.divDisplayPagenation;
  DHTMLpagenation.contentStyle=null;
  DHTMLpagenation.strDisplayContent="";
  DHTMLpagenation.strDisplayPagenation="";
  
  // 把第二个参数赋给perpageLength;
    arguments.length==2?perpageLength=arguments[1]:'';

  try
  {
   divExecuteTime=document.createElement("div");
   document.body.appendChild(divExecuteTime);
  }
  catch(e)
  {
   
  }

  // 得到divPagenation容器。
  if(document.getElementById("divPagenation"))
  {
   divDisplayPagenation=document.getElementById("divPagenation");
  }
  else
  {
   try
   {
    divDisplayPagenation=document.createElement("div");
    divDisplayPagenation.id="divPagenation";
    document.body.appendChild(divDisplayPagenation);
   }
   catch(e)
   {
    return false;
   }
    }

  // 得到divContent容器
  if(document.getElementById("divContent"))
  {
   divDisplayContent=document.getElementById("divContent");
  }
  else
  {
   try
   {
    divDisplayContent=document.createElement("div");
    divDisplayContent.id="divContent";
    document.body.appendChild(divDisplayContent);
   }
   catch(e)
   {
    return false;
   }
  }
  DHTMLpagenation.initialize();
  return DHTMLpagenation;
   }
};

//初始化分页;
//包括把加入CSS,检查是否需要分页
DHTMLpagenation.initialize=function()
{
 with (DHTMLpagenation)
 {
  divDisplayContent.className=contentStyle!=null?contentStyle:"divContent";
  if(contentLength<=perpageLength)
  {
   strDisplayContent=content;
   divDisplayContent.innerHTML=strDisplayContent;
   return null;
  }
  
  pageSizeCount=Math.ceil((contentLength/perpageLength));
  DHTMLpagenation.gotopage(currentPage);
  DHTMLpagenation.displayContent();
 }
};


//显示分页栏
DHTMLpagenation.displayPage=function()
{
 with (DHTMLpagenation)
 {
  var a = document.location.toString();  
  var b = a.lastIndexOf("/")+1;  
  var c = a.substr(b).split(".");  
  var url = c[0]+"."+ c[1];

  //alert(url);

    strDisplayPagenation="";

  if(currentPage&&currentPage!=1)
    strDisplayPagenation+='<a href="#" onclick="DHTMLpagenation.previous();">上一页</a>&nbsp;&nbsp;';
  else
    strDisplayPagenation+="&nbsp;&nbsp;";

  for(var i=1;i<=pageSizeCount;i++)
  {
    if(i!=currentPage)
      strDisplayPagenation+='<a href="#" onclick="DHTMLpagenation.gotopage('+i+');">['+i+']</a>&nbsp;&nbsp;';
    else
      strDisplayPagenation+=i+"&nbsp;&nbsp;";
  }

  if(currentPage&&currentPage!=pageSizeCount)
  {
    strDisplayPagenation+='<a href="#" onclick="DHTMLpagenation.next();">下一页</a>&nbsp;&nbsp;';
  }
 
  else
  {
    strDisplayPagenation+="&nbsp;&nbsp;";

  //strDisplayPagenation+="共 " + pageSizeCount + " 页";
  }

 

  divDisplayPagenation.innerHTML=strDisplayPagenation;
}};
//上一页
DHTMLpagenation.previous=function() { with(DHTMLpagenation)
{
  DHTMLpagenation.gotopage(currentPage-1);
}};
//下一页
DHTMLpagenation.next=function() { with(DHTMLpagenation)
{
  DHTMLpagenation.gotopage(currentPage+1);
}};
//跳转至某一页
DHTMLpagenation.gotopage=function(iCurrentPage) { with (DHTMLpagenation)
{
  startime=new Date();
  if(regularExp.test(iCurrentPage))
  {
    currentPage=iCurrentPage;
    strDisplayContent=content.substr((currentPage-1)*perpageLength,perpageLength);
  }
  else
  {
    alert("page parameter error!");
  }
  DHTMLpagenation.displayPage();
  DHTMLpagenation.displayContent();
}};
//显示当前页内容
DHTMLpagenation.displayContent=function() { with (DHTMLpagenation)
{
  divDisplayContent.innerHTML=strDisplayContent;
}};
//改变每页的字节数
DHTMLpagenation.change=function(iPerpageLength) { with(DHTMLpagenation)
{
  if(regularExp.test(iPerpageLength))
  {
    DHTMLpagenation.perpageLength=iPerpageLength;
    DHTMLpagenation.currentPage=1;
    DHTMLpagenation.initialize();
  }
  else
  {
    alert("请输入数字");
  }
}};

// 接口API
// DHTMLpagenation(strContent[,perpageLength])

DHTMLpagenation(s,800);
</script>

原创粉丝点击