时间:2022-01-21 09:43:57 | 栏目:JavaScript代码 | 点击:次
</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>