复制代码 代码如下:
<!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>读取Xml并分页</title>
</head>
<body >
<div id="gao">
</div>
<div>
<table align="center">
<tr>
<td><input type="button" name="prev" id="prev" value="上一页" onclick="showPage('false')"><input type="button" id="next" name="next" value="下一页" onclick="showPage('true')"> </td>
</tr>
</table>
</div>
<script type="text/javascript">
function LoadXML(url)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load(url);
}else if(document.implementation&&document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load(url);
}else
{
alert("You browser cannot handle this script!");
}
return xmlDoc;
}
var xmlDoc=LoadXML("student.xml");
var students=xmlDoc.getElementsByTagName("student");
var stuLength=students.length;
var currentPage=0;
var pageSize=2;
var maxPage=Math.ceil(stuLength/pageSize);
function showPage(page)
{
var mytable=document.getElementsByTagName("table")[1];
if(mytable) document.body.removeChild(mytable);
var table=document.createElement("table");
table.setAttribute("id","mytable");
table.setAttribute("width","600");
table.setAttribute("border","1");
document.body.appendChild(table);
var header=table.createTHead();
var headerrow=header.insertRow(0);
headerrow.insertCell(0).appendChild(document.createTextNode("姓名"));
headerrow.insertCell(1).appendChild(document.createTextNode("学校"));
headerrow.insertCell(2).appendChild(document.createTextNode("成绩"))
if(page=="true")
currentPage++;
else
currentPage--;
if(currentPage>=maxPage) currentPage=maxPage;
else if(currentPage<=0) currentPage=1;
var start=(currentPage-1)*pageSize;
var end=currentPage*pageSize-1;
for(var i=start;i<=end;i++)
{
var e=students[i];
var name=e.getAttribute("name");
var school=e.getElementsByTagName("school")[0].firstChild.data;
var grade=e.getElementsByTagName("grade")[0].firstChild.data;
var row=table.insertRow(i%pageSize+1);
row.insertCell(0).appendChild(document.createTextNode(name));
row.insertCell(1).appendChild(document.createTextNode(school));
row.insertCell(2).appendChild(document.createTextNode(school));
}
}
showPage("true");
</script>
</body>
</html>
在编写过程过,涉及到代码的处理上,由于我最初,将javascript代码放在了head标签里面,结果导致document.body对象为空!
我们都知道,javascript在执行的时候,页面会暂定加载而去执行html代码,所以当js代码放在head标签中的时候,html页面还没有执行到body,故而产生了错误!
后来将js代码,放在了body的尾部,这样,就能够正确引用到body对象了!
同时,在大型网站架构中,也是提高页面加载速度的一种方式!页面首页加载html标签内容,到页面最后,在执行js代码,能在很大程度上提升网页打开速度,提升用户体验!
另外的student.xml内容是
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<students>
<student name="gaoxing">
<school>西北大学1</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大学2</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大3学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大学4</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大5学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大6学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大7学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北8大学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北9大学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北10大学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大学</school>
<grade>76</grade>
</student>
<student name="gaoxing">
<school>西北大学</school>
<grade>76</grade>
</student>
</students>
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 曾庆瑜1990-随风而逝[日本东芝1A1首版][WAV+CUE]
- 群星.2015-凭着爱ADMS2CD【华纳】【WAV+CUE】
- 陈冠希.2017-一只猴子3部曲【摩登天空】【WAV+CUE】
- 金元萱.1996-迷迷糊糊【宝丽金】【WAV+CUE】
- 齐秦《燃烧爱情》马来西亚版[WAV+CUE][1G]
- 动力火车《结伴》2024最新 [FLAC分轨][1G]
- 郑源《擦肩而过》[WAV+CUE][1.2G]
- 黑鸭子2008-江南四月天[首版][WAV+CUE]
- 黑鸭子2008-再醉一次·精选[首版][WAV+CUE]
- Elgar-Motdamour-UlfWallin,RolandPontinen(2024)[24bit-96kHz]FLAC
- 苏永康《 笑下去》 新曲+精选[WAV+CUE][1G]
- 周传雄《发觉》[WAV+CUE][1.1G]
- 证声音乐图书馆《真夏派对 x 浩室》[320K/MP3][67.19MB]
- 张镐哲.1994-无助【波丽佳音】【WAV+CUE】
- Relic.2024-浮在虛无的诗意【SEEAHOLE】【FLAC分轨】