一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

js原生态ajax XMLHttpRequest仿百度智能搜索功能

时间:2014-07-16 编辑:简简单单 来源:一聚教程网

html代码

 代码如下 复制代码

      

    js ajax代码如下

     代码如下 复制代码

    function createXMLHttpRequest(){//创建XMLHttpRequest对象
    if(window.ActiveXObject){//IE
    try {
    return new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e){
    return;
    }
    }else if(window.XMLHttpRequest){//Mozilla,firefox
    try {
    return new XMLHttpRequest();
    } catch(e){
    return;
    }
    }
    }

    function getreNews(Value){//主调函数
    var xmlHttp=createXMLHttpRequest();
    var url = "ajaxSend.php?title="+Value+"&mt="+Math.random(300000);
    if (Value==""){
    return false ;
    }

    if (xmlHttp){
    callback = getReadyStateHandler(xmlHttp);
    xmlHttp.onreadystatechange = callback;
    xmlHttp.open("GET", url,true);
    xmlHttp.send(null);
    }
    }

    function getReadyStateHandler(xmlHttp){//服务器返回后处理函数
    return function (){
    if(xmlHttp.readyState == 4){
    if(xmlHttp.status == 200){
    if (xmlHttp.responseText){
    document.getElementById("apDiv1").style.display='block';
    document.getElementById("apDiv1").innerHTML=xmlHttp.responseText;
    }else{
    document.getElementById("apDiv1").innerHTML="

  • 对不起,未找到相对应楼盘!
  • ";
    }
    }
    }
    }
    }  

    上面这代码是在百度搜索到了自己简单的进行修改了一下,因为自己以前用过所以觉得使用快也就没使用jquery ajax插件了。

     

    热门栏目