JavaScript to operate the XML instance code (obtain the news title, pagination, and pagination)

Source: Internet
Author: User

I did not perform the test. For reference only
Code Copy code The Code is as follows: <? XML version = "1.0" encoding = "gb2312"?>
<News>
<New id = "1" name = "test News 1" time = "2010-2-18">
<Nbody> News test 1 News test 1 </nbody>
</New>
<New id = "2" name = "test News 2" time = "2010-2-18">
<Nbody> News test 2 News test 2 </nbody>
</New>
<New id = "3" name = "test NEWS 3" time = "2010-2-18">
<Nbody> News Test 3 News Test 3 </nbody>
</New>
<New id = "4" name = "test News 4" time = "2010-2-18">
<Nbody> News test 4 News test 4 </nbody>
</New>
<New id = "5" name = "test News 5" time = "2010-2-18">
<Nbody> News Test 5 news Test 5 </nbody>
</New>
<New id = "6" name = "test news 6" time = "2010-2-18">
<Nbody> News Test 6 News Test 6 </nbody>
</New>
<New id = "7" name = "test News 7" time = "2010-2-18">
<Nbody> News Test 7 News Test 7 </nbody>
</New>
<New id = "8" name = "test News 8" time = "2010-2-18">
<Nbody> News test 8 News test 8 </nbody>
</New>
<New id = "9" name = "test News 9" time = "2010-2-18">
<Nbody> News test 9 News test 9 </nbody>
</New>
</News>

JS Code section. Some JavaScript code is relatively simple. Important XML operation functions Copy code The Code is as follows: // Javascript
Function bindnew (INDEX)
{
VaR list = new array ();
List = showpage (INDEX );
VaR listul = "<ul style = \" font-size: 12px \ ">"
For (VAR I = 0; I <list. length; I ++)
{
// Document. getelementbyid ("listtb"). innerhtml + = list [I] + "</BR> ";
Listul + = "<li style = \" margin-top: 4px \ ">" + list [I] + "</LI> ";
}
Listul + = "</ul> ";
Document. getelementbyid ("listtb"). innerhtml = listul;
Document. getelementbyid ("refer"). innerhtml = "";
VaR Tc = rtnpagecount ();
For (VAR I = 1; I <= tc; I ++)
{
// <Div style = "width: 14px; Height: 12px; color: # ffffff; Background-color: #000000; float: Left"> </div>
Document. getelementbyid ("refer "). innerhtml + = "<Div style = \" width: 14px; Height: 14px; color: # ffffff; Background-color: #000000; float: Left; cursor: hand \ "onclick = \" bindnew ("+ I +") \ ">" + I + "</div> ";
}
}
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = false;
Xmldoc. resolveexternals = false;
Xmldoc. Load ("news. xml ");
// Return the news with a fixed ID
Function findnew (newid)
{
VaR domelement = xmldoc.doc umentelement;
Alert (domelement );
}
// Returns the connection of the news title after the page.
VaR pagesize = 40;
Function showpage (pageindex)
{
VaR alltt = new array ();
Alltt = alltitlelink ();
VaR rtlinks = new array ();
/* Unfinished parts */
Try
{
For (VAR I = 0; I <pagesize & (pagesize * (pageIndex-1) + I) <alltt. length; I ++)
{
Rtlinks [I] = alltt [pagesize * (pageIndex-1) + I];
}
}
Catch (E)
{Alert ("showpage () method error! ");}
Return rtlinks;
}
// Number of pages after pagination
Function rtnpagecount ()
{
VaR countx = getcount ();
If (countx % pagesize = 0)
{
Return countx/pagesize;
}
Else
{
Return countx/pagesize + 1;
}
}
// Number of returned news records
Function getcount ()
{
VaR newroot = xmldoc.doc umentelement;
VaR allnews = newroot. childnodes;
Return allnews. length;
}
// Returns a fixed number of news title connections
Function returnnews (count)
{
VaR titles = new array ();
VaR J = 0;
Newroot = xmldoc.doc umentelement;
Allnews = newroot. childnodes;
For (VAR I = 0; I <allnews. length; I ++)
{
If (I> = allnews. lenth)
{
Titles [J] = "<a herf = \" ../news.html? Id = "+ (I + 1) +" \ ">" + allnews [I]. getarrtibute ("name") + "</a> ";
J ++;
}
}
Return titles;
}
// Send back the hyperconnections of all news titles
Function alltitlelink ()
{
VaR allttlink = new array ();
VaR newroot = xmldoc.doc umentelement;
VaR allnews = newroot. childnodes;
For (VAR I = 0; I <allnews. length; I ++)
{
VaR context = allnews [I];
Allttlink [I] = "<a href = \" ../news.html? Id = "+ (I + 1) +" \ ">" + context. getattribute ("time") + ":" + context. getattribute ("name") + "</a> ";
}
Return allttlink;
}

Some HTML codeCopy codeThe Code is as follows: <Table width = "90%" border = "0" cellspacing = "0" cellpadding = "0" style = "font-size: 12px; text-align: left ">
<Tr>
<TD> <Div style = "width: 400px; border-bottom: 1px dotted #999999; color: #333333; font-weight: bold "> All dynamics >></div> </TD>
</Tr>
<Tr>
<TD>
<Div id = "listtb" class = "listul"> </div> </TD>
</Tr>
<Tr>
<TD>
<Div id = "refer"> </div> </TD>
</Tr>
</Table>

I hope to help you in your learning work. You are welcome to ask for feedback.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.