zoukankan      html  css  js  c++  java
  • 一个ajax的例子

    html :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <script type="text/javascript" src="xmlhttprequest.js"></script>
    </HEAD>

    <BODY>
    <div id="see">

    </div>
    <input type="button" onclick="sendRequest()" value="SendRequest">
    <script>

    var xmlhttp = new XMLHttpRequest();
    var xmldoc;
    function sendRequest(){  
      document.all("see").innerHTML = "Loading....";  
      var url = "http://localhost:8080/tree.xml";
      xmlhttp.open("GET",url,true);
      xmlhttp.onreadystatechange = viewPage;  
      xmlhttp.send(null);
    }
    function viewPage(){  
      
      if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
       alert("haha get xml ")
       xmldoc = xmlhttp.responseXML;
       var xmlobj = xmldoc.documentElement;
       var str = "";
       for(var i = 0; i< xmlobj.childNodes.length;i++){
        var node = xmlobj.childNodes[i];
        var text = node.getAttribute("nodeid");
        var value =  text;
        str += "<span style='display:block'  onclick='getsub(\""+value+"\")'>"+text+"</span><br><div style='display:none' id="+text+"></div>";
       }
       document.all.see.innerHTML = str;
       //alert(xmlobj.tagName)
      }
    }

    function getsub(id){
      if(xmldoc!=null){
       var root = xmldoc.getElementsByTagName("TreeNode");
       var str2 = "";
       for(var k = 0;k<root.length;k++){    
        if(root[k].getAttribute("nodeid")==id){
         document.all(id).style.display = '';
         document.all(id).innerHTML = "loading..."
         alert("get sub tree")
         var nodes = root[k].childNodes;
         for(var m = 0;m<nodes.length;m++){
          var node = nodes[m];
          var text = node.getAttribute("nodeid");      
          str2 += "<span  onclick='getsub(\""+text+"\")'>&nbsp;&nbsp;"+text+"</span><br><div style='display:none' id="+text+"></div>";
         }    
         document.all(id).innerHTML = str2 ;//alert(document.all(id).innerHTML)
        }
        
       }
      }else{
       alert("Exception!!");
      }
    }

    </script>
    </BODY>
    </HTML>
    需要访问的xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <Tree>
    <TreeNode nodeid="wangzhongjie">
    <TreeNode nodeid="wang"></TreeNode>
    <TreeNode nodeid="zhong"></TreeNode>
    <TreeNode nodeid="jie"></TreeNode>
    </TreeNode>
    <TreeNode nodeid="yinhang">
    <TreeNode nodeid="yin"></TreeNode>
    <TreeNode nodeid="hang"></TreeNode>
    </TreeNode>
    </Tree>

  • 相关阅读:
    http请求类型简介
    关于PLSQL连接报错:ORA-12154:TNS:无法解析指定的连接标识符
    JDK8的安装与配置
    今天新装tomcat遇到黑窗口(startup.bat)启动乱码问题解决!!!
    一个简单的工厂模式(一个接口,多个实现,通过调用条件的不同,分别去调用符合的实现)
    数组(复习)
    java选择结构、循环结构(复习)
    java常用的数据类型,变量和常量,运算符(复习)
    java输入输出,书写规范,运行原理,跨平台原理(复习)
    JAVA基础入门(JDK、eclipse下载安装)
  • 原文地址:https://www.cnblogs.com/zhuor/p/306475.html
Copyright © 2011-2022 走看看