Ajax achieves Tab switching

Source: Internet
Author: User

The helloworld of Ajax will be written, and I will enter the door. Let's take a look. This time I am going to write a tab switch effect. It used to be the first time the page was loaded, so I will read all the frames, then, an event is triggered to display or disappear the layer. This time I want to come up with an Ajax version, that is, loading the content of only the first frame of the page for the first time, and then moving the mouse over and switching frames to asynchronously read data is that simple.

 

Program Design ideas:

1. When the page is loaded for the first time, bind the window. onload event to asynchronously read the content in the works_0.xml file and display it. One frame is an XML file, and frame switching is actually to read different XML files.

2. move the cursor to trigger an asynchronous request and read the corresponding XML file.

3. parse XML file content

4. Splice HTML strings

5. Use the innerhtml method to display it to the specified Div

 

Easy to ignore:

1. If you move the cursor to the current tag, the request should not be triggered and a judgment should be made.

2. Each frame of a request is requested only once, and then the data is saved. Before triggering the request, the system should judge whether the request is triggered for the first time. If the request is not the first time, no request is made, directly display the results saved in the first request. I used a hidden on the page to save the result to its value. Of course there are other methods.

3. When loading XML strings for processing, the IE and FF methods are different. Note that I searched for a common function on the Internet and used javas_^.

4. It seems there is nothing .......

 

 

 

HTML code:

 

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <br/> <HTML xmlns = "http://www.w3.org/1999/xhtml"> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html" charset = "UTF-8"/> <br/> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <title> display of mytino monthly outstanding member Works </title> <br/> <link type = "text/CSS "href =" CSS/reset.css "mce_href =" CSS/reset. CS S "rel =" stylesheet "Media =" all "/> <br/> <link type =" text/CSS "href =" CSS/default.css "mce_href =" CSS/default.css "rel =" stylesheet "Media =" all "/> <br/> <! -- [If IE 7]> <br/> <link type = "text/CSS" href = "CSS/ie7.css" mce_href = "CSS/ie7.css" rel = "stylesheet" Media = "screen"/> <br/> <! [Endif] --> <br/> <! -- [If IE 6]> <br/> <link type = "text/CSS" href = "CSS/ie6.css" mce_href = "CSS/ie6.css" rel = "stylesheet" Media = "screen"/> <br/> <! [Endif] --> <br/> <MCE: Script Type = "text/JavaScript" src = "JS/works. JS "mce_src =" JS/works. JS "> </MCE: SCRIPT> <br/> <? PHP include_once ("../header_index.php");?> <Br/> <Div id = "Wrap"> <br/> <Div id = "Header"> </div> <br/> <Div id = "bodycontent"> <br/> <Div id = "tabs" class = "Clearfix"> <br/> <ul> <br/> <li class = "curtab" onmouseover = "changetab (this, 'Works _ 0'); "> <H2> excellent works of the last 7 days </H2 & gt; </LI> <br/> <li onmouseover =" changetab (this, 'Works _ 1'); "> <H2> excellent works of this month </H2> </LI> <br/> <li onmouseover =" changetab (this, 'Works _ 2 '); "> <H2> randomly recommended Works </H2> </LI> <br/> </ul> <br/> </div> <br/> <Div id = "workscontent"> <br/> <ul id = "works" class = "works Clearfix"> </P> <p> </ul> <br/> </div> <br/> <Div id = "footer"> </div> <br/> <? PHP require_once (".../../templates/foot. php")?> <Br/> </div> <br/> <input type = "hidden" id = "works_0" value = ""/> <br/> <input type = "hidden "id =" works_1 "value =" "/> <br/> <input type =" hidden "id =" works_2 "value =" "/> <br/> <? PHP include_once (".../../commonphp/google_analytics.php");?> </P> <p> </body> <br/> </ptml>

 

 

CSS style:

 

/* General */<br/> body {<br/> text-align: center; <br/> Background: # f1f1f1; <br/> width: 980px; <br/>}< br/>/* layerout */<br/> # wrap {<br/> width: 980px; <br/> margin: 10px auto; <br/> text-align: Left; <br/>}< br/># header {<br/> Background: URL (.. /images/works_header.gif); <br/> Height: 180px; <br/>}< br/> # bodycontent {<br/> margin-bottom: 10px; <br/> border: solid 1px # CCC; <br/>}< br/>. last {<br/> margin-Right: 0; <br/>}< br/>/* clear both */<br/>. clearfix {display: inline-block;} <br/>/* hides from IE-MAC/*/<br/> * HTML. clearfix {Height: 1% ;}< br/>. clearfix {display: block;} <br/>/* end hide from IE-Mac */<br/>. clearfix: After {<br/> content :". "; <br/> display: block; <br/> Height: 0; <br/> clear: Both; <br/> visibility: hidden <br/>}< br/>/* end layerout */<br/>/* @ Navigation */<br/> # tabs {<br/> display: inline-block; <br/>}< br/> # tabs Li {<br/> Background: URL (".. /images/tab_bg.gif ") Repeat-X; <br/> float: Left; <br/> width: 200px; <br/> Height: 33px; <br/> border-left: solid 1px # FFF; <br/> border-Right: solid 1px # cecece; <br/> cursor: pointer; <br/>}< br/> # tabs H2 {<br/> Height: 33px; <br/> line-Height: 33px; <br/> text-align: center; <br/>}< br/> # tabs. curtab {<br/> Background: # ffffff; <br/> border-Right: 0; <br/> font-weight: bold; <br/>}< br/>/* end Navigation */<br/>/* @ workscontent */<br/> # workscontent {<br/> padding: 15px; <br/> _ padding: 10px; <br/> background-color: # FFF; <br/>}< br/> # works Li {<br/> float: left; <br/> width: 186px; <br/> Height: 134px; <br/> margin: 5px; <br/> _ margin: 4px; <br/> padding: 20px; <br/> Background: URL (".. /images/work_bg.gif "); <br/>}< br/> # workscontent. works {</P> <p >}< br/>/* end workscontent */

 

JS Code:

/** <Br/> * @ author Liuyong <br/> */<br/> var XMLHTTP; <br/> var xmldoc; <br/> var works_0 = ''; <br/> var works_1 =''; <br/> var works_2 = ''; // flag variable, store content_str spelled out by the first request <br/> var divid = ''; <br/> function loadxmlstr (strxml) {// general functions compatible with loading XML strings of IE and FF <br/> try {<br/> xmldoc = new activexobject ("Microsoft. xmldom "); <br/> xmldoc. loadxml (strxml); <br/>}< br/> catch (e) {<br/> var oparser = new domparser (); <B R/> xmldoc = oparser. parsefromstring (strxml, "text/XML"); <br/>}< br/> return xmldoc; <br/>}< br/> function createxmlhttprequest () {<br/> If (window. activexobject) {/* initialize the XMLHTTPRequest object in IE */<br/> try {<br/> XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); // new version of IE <br/>} catch (othermicrosoft) {<br/> try {<br/> // older version of Internet Explorer <br/> XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); <br/>} Catch (failed) {<br/> // still failed, it is considered that the creation failed ...... <Br/> XMLHTTP = false; <br/>}< br/>} else if (window. XMLHttpRequest) {<br/> XMLHTTP = new XMLHttpRequest (); <br/>}< br/> If (! XMLHTTP) {<br/> alert ("An error occurred while creating the XMLHTTPRequest object! "); <Br/>}< br/> function startrequest (filename) {</P> <p> divid = filename; <br/> createxmlhttprequest (); <br/> try {<br/> If (document. getelementbyid (filename ). value = '') {<br/> XMLHTTP. onreadystatechange = handlestatechange; <br/> XMLHTTP. open ("get", "Data/" + filename + ". XML ", true); <br/> XMLHTTP. send (null); <br/>}else {<br/> document. getelementbyid ("works "). innerhtml = document. getel Ementbyid (filename). value; <br/>}< br/>} catch (exception) {<br/> alert ("The resource you want to access does not exist! "); <Br/>}< br/> function handlestatechange () {<br/> If (XMLHTTP. readystate = 4) {<br/> If (XMLHTTP. status = 200 | XMLHTTP. status = 0) {<br/> // get the xml dom object <br/> // alert (XMLHTTP. responsetext); <br/> xmldoc = loadxmlstr (XMLHTTP. responsetext); <br/> // get the root of the XML document <br/> var root = xmldoc.doc umentelement; <br/> try <br/> {<br/> // obtain the <Works> result <br/> var works = root. getelementsbytagname ('work '); <Br/> // alert (works. length); <br/> var userid; <br/> var foldid; <br/> var workid; <br/> var content_str = ''; // spell the HTML string <br/> for (I = 0; I <works. length; I ++) {<br/> userid = works [I]. getelementsbytagname ("userid") [0]. firstchild. nodevalue; <br/> foldid = works [I]. getelementsbytagname ("foldid") [0]. firstchild. nodevalue; <br/> workid = works [I]. getelementsbytagname ("workid") [0]. firstchild. nodeva Lue; <br/> content_str = content_str + '<li> <a href = "/" mce_href = "/" "http://www.mytino.com/cn/profile/profile_works_detail.php? Workid = '+ workid +' & foldid = '+ foldid +' & type = 2 & userid = '+ userid +'/"target =/" _ blank/"> </a> </LI> '; </P> <p >}< br/> document. getelementbyid ("works "). innerhtml = content_str; <br/> document. getelementbyid (divid ). value = content_str; <br/> divid = ''; <br/>} catch (exception) <br/>{< br/> alert (" exception! "); <Br/>}< br/>} else {<br/> document. getelementbyid ("works "). innerhtml = ' '; <br/>}</P> <p >}< br/> function changetext (STR, element) {<br/> If (navigator. appname. indexof ("Explorer")>-1) {<br/> document. getelementbyid (element ). innertext = STR; <br/>}else {<br/> document. getelementbyid (element ). textcontent = STR; <br/>}< br/> var arr_li; <br/> function changetab (ARG, con) {<br/> arr_li = document. getelementbyid ("tabs "). getelementsbytagname ("Li"); <br/> for (VAR I = 0; I <arr_li.length; I ++) {<br/> arr_li [I]. classname = 'none'; <br/>}< br/> Arg. classname = 'curtab'; <br/> If (Arg. classname = 'curtab') {<br/> If (con = 'Works _ 0') {<br/> startrequest ('Works _ 0 '); <br/>} else if (con = 'Works _ 1') {<br/> startrequest ('Works _ 1 '); <br/>} else if (con = 'Works _ 2') {<br/> startrequest ('Works _ 2 '); <br/>}< br/> window. onload = function () {<br/> startrequest ('Works _ 0'); <br/>}

 

 

XML file:

 

Works_0.xml

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Work>
<Userid> 97026 </userid>
<Foldid> 3893 </foldid>
<Workid> 8379 </workid>
</Work>
<Work>
<Userid> 97026 </userid>
<Foldid> 3893 </foldid>
<Workid> 8378 </workid>
</Work>
<Work>
<Userid> 97026 </userid>
<Foldid> 3893 </foldid>
<Workid> 8374 </workid>
</Work>
<Work>
<Userid> 97026 </userid>
<Foldid> 3893 </foldid>
<Workid> 8369 </workid>
</Work>
<Work>
<Userid> 358611 </userid>
<Foldid> 14049 </foldid>
<Workid> 33964 </workid>
</Work>
<Work>
<Userid> 358611 </userid>
<Foldid> 14046 </foldid>
<Workid> 33960 </workid>
</Work>
<Work>
<Userid> 358611 </userid>
<Foldid> 14046 </foldid>
<Workid> 33959 </workid>
</Work>
<Work>
<Userid> 358542 </userid>
<Foldid> 14033 </foldid>
<Workid> 33951 </workid>
</Work>
</Root>

Works_1.xml

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Work>
<Userid> 358457 </userid>
<Foldid> 14027 </foldid>
<Workid> 33946 </workid>
</Work>
<Work>
<Userid> 358413 </userid>
<Foldid> 14025 </foldid>
<Workid> 33936 </workid>
</Work>
<Work>
<Userid> 358413 </userid>
<Foldid> 14025 </foldid>
<Workid> 33935 </workid>
</Work>
<Work>
<Userid> 358413 </userid>
<Foldid> 14025 </foldid>
<Workid> 33934 </workid>
</Work>
<Work>
<Userid> 358458 </userid>
<Foldid> 14024 </foldid>
<Workid> 33931 </workid>
</Work>
<Work>
<Userid> 233263 </userid>
<Foldid> 8049 </foldid>
<Workid> 33927 </workid>
</Work>
<Work>
<Userid> 162003 </userid>
<Foldid> 14016 </foldid>
<Workid> 33925 </workid>
</Work>
<Work>
<Userid> 162003 </userid>
<Foldid> 14015 </foldid>
<Workid> 33919 </workid>
</Work>
</Root>

Works_2.xml

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Work>
<Userid> 358818 </userid>
<Foldid> 14082 </foldid>
<Workid> 34050 </workid>
</Work>
<Work>
<Userid> 358818 </userid>
<Foldid> 14082 </foldid>
<Workid> 34045 </workid>
</Work>
<Work>
<Userid> 196211 </userid>
<Foldid> 5503 </foldid>
<Workid> 11983 </workid>
</Work>
<Work>
<Userid> 92431 </userid>
<Foldid> 13756 </foldid>
<Workid> 33296 </workid>
</Work>
<Work>
<Userid> 358704 </userid>
<Foldid> 14089 </foldid>
<Workid> 34060 </workid>
</Work>
<Work>
<Userid> 359066 </userid>
<Foldid> 14094 </foldid>
<Workid> 34070 </workid>
</Work>
<Work>
<Userid> 358656 </userid>
<Foldid> 14061 </foldid>
<Workid> 33998 </workid>
</Work>
<Work>
<Userid> 359206 </userid>
<Foldid> 14104 </foldid>
<Workid> 34089 </workid>
</Work>
</Root>

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.