欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

原生Ajax 和jQuery Ajax的区别示例分析

程序员文章站 2022-11-30 23:42:04
  前言:这次介绍的是利用ajax与后台进行数据交换的小例子,所以demo必须通过服务器来打开。服务器环境非常好搭建,从网上下载wamp或xampp,一步步安装就ok,然后再把写好...

  前言:这次介绍的是利用ajax与后台进行数据交换的小例子,所以demo必须通过服务器来打开。服务器环境非常好搭建,从网上下载wamp或xampp,一步步安装就ok,然后再把写好的页面放在服务器中指定的位置。打开时,在地址栏输入“localhost/指定页面”或者“127.0.0.1/指定页面”打开。

  下面列出demo的html、php、原生ajax 、jq ajax代码。

  html代码:

代码如下:


<!doctype html>
<html>
<head>
    <title>ajax示例</title>
    <meta charset='utf-8' />
    <link rel="stylesheet" type="text/css" href="css/common.css" />
    <style type="text/css">
        .main{height:400px;width:800px;margin:100px auto 0;border:1px solid #000;}
        .list{height:400px;width:200px;float:left;background:#ddd;}
        .inf{height:400px;width:600px;float:right;background:#ccc;text-align:center;}
        .list li{width:200px;text-align:center;margin:50px 0 0;font-size:24px;cursor: pointer;
        }
        .inf img{width:360px;height:270px;margin:15px auto;}
        .inf p{width:580px;text-align:left;text-indent:2em;font-size:14px;margin:0 10px;}
    </style>
</head>
<body>
    <p class='main'>
        <p class='list' id='list'>
            <ul>
                <li name='spring' id='spring'>春</li>
                <li name='summer' id='summer'>夏</li>
                <li name='fall' id='fall'>秋</li>
                <li name='winter' id='winter'>冬</li>
            </ul>
        </p>
        <p class='inf' id='inf'>
        <!--要插入的内容-->
        </p>
    </p>
</body>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
</html>

 

  php代码:

 

代码如下:


<?php
$details = array (
    'spring'    =>    "<img src='images/spring.jpg' alt='' /><p>人间四月芳菲尽,山寺桃花始盛开</p>",
    'summer'    =>    "<img src='images/summer.jpg' alt='' /><p>水晶帘动微风起,满架蔷薇一院香</p>",
    'fall'    =>    "<img src='images/fall.jpg' alt='' /><p>金井梧桐秋叶黄,珠帘不卷夜来霜</p>",
    'winter'        =>    "<img src='images/winter.jpg' alt='' /><p>梅须逊雪三分白,雪却输梅一段香</p>"
);
echo $details[$_request['liname']];
?>

 

  原生ajax:

 

代码如下:


<script type="text/javascript">
    var lis = document.getelementbyid('list').getelementsbytagname('li');
    window.onload = initpage;
    function initpage() {
        for (var i=0; i<lis.length; i++) {
            txt = lis[i];
            txt.onclick = function () {
                getdetails(this.id);
            }
        }
    }
    function creatrequest() {
        try {
            request = new xmlhttprequest();
        }
        catch (tryms) {
            try {
                request = new activexobject("msxml2.xmlhttp");
            }
            catch (otherms) {
                try {
                    request = new activexobject("miscrosoft.xmlhttp");
                }
                catch (failed) {
                    request = null;
                }
            }
        }
        return request;
    }
    function getdetails(itemname) {
        request = creatrequest();
        if (request == null) {
            alert('没有成功创建请求')
            return;
        }
        var url = "getdetails.php?liname="+escape(itemname);
        request.open("get",url,true);
        request.onreadystatechange = displaydetails;
        request.send(null);
    }
    function displaydetails() {
        if (request.readystate == 4) {
        if (request.status == 200) {
            detaildiv = document.getelementbyid("inf");
            detaildiv.innerhtml = request.responsetext;
        }
      }
    }  
</script>

 

  jq ajax:

 

代码如下:


<script type="text/javascript">
$('#list li').click ( function () {                      
        $.ajax({                          
            type:'get',                           
            data:'',                          
            url:"getdetails.php?liname="+this.id,                          
            success:function(data){                               
                $('#inf').html(data);                               
            },
            datatype:'text',
            error:function (){               
                alert("失败!");           
            }
        })                   
    });
</script>