1. 程式人生 > >jq程式碼學習19----- jq中的ajax全域性事件

jq程式碼學習19----- jq中的ajax全域性事件

jq簡化AJAX操作不僅體現在呼叫AJAX方法和處理響應方面,而且還體現在對呼叫AJAX方法的過程中的http請求的控制,通過jq提供的一些自定義全域性函式,能能夠為各種與ajax請求結束時,會觸發ajaxStop()方法的回撥函式。這些方法都是全域性函式,因此無論建立它們的程式碼位於何處,只要發生ajax請求,就會觸發他們。
demo.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> * { margin:0; padding:0;} body { font-size:12px;} #loading{ width:80px; height: 20px; background:#bbb; color
:#000
; display:none; }
img{border:0;height:100px;width:100px;} .comment { margin-top:10px; padding:10px; border:1px solid #ccc;background:#DDD;} .comment h6 { font-weight:700; font-size:14px;} .para { margin-top:5px; text-indent:2em;background:#DDD;}
</style> <!-- 引入jQuery --> <script src="../scripts/jquery.js"
type="text/javascript">
</script> <script type="text/javascript"> //<![CDATA[ $(function(){ //demo1: $('#send1').click(function() { $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=car&tagmode=any&format=json&jsoncallback=?", function(data){ $("#resText1").empty(); $.each(data.items, function( i,item ){ $("<img/> ").attr("src", item.media.m ).appendTo("#resText1"); if ( i == 3 ) { return false; } }); } ); }); //demo2: $("#send2").click(function(){ $.get("get1.php", { username : $("#username").val() , content : $("#content").val() }, function (data, textStatus){ $("#resText2").html(data); // 把返回的資料新增到頁面上 } ); }) $.ajaxPrefilter(function( options ) { options.global = true; }); //共用這2個全域性的ajax事件 $("#loading").ajaxStart(function(){ $(this).show(); }); $("#loading").ajaxStop(function(){ $(this).hide(); }); }) //]]> </script> </head> <body> <br/> <div id="loading">載入中...</div> <br/> Demo1: <br/> <input type="button" id="send1" value="載入"/> <div id="resText1" ></div> <br/> Demo2: <br/> <form id="form1" action="#"> <p>評論:</p> <p>姓名: <input type="text" name="username" id="username" /></p> <p>內容: <textarea name="content" id="content" rows="2" cols="20"></textarea></p> <p><input type="button" id="send2" value="提交"/></p> </form> <div class='comment'>已有評論:</div> <div id="resText2" > </div> </body> </html>

get1.php

<?php 
    header("Content-Type:text/html; charset=utf-8");
    echo "<div class='comment'><h6>{$_REQUEST['username']}:</h6><p class='para'>{$_REQUEST['content']}</p></div>";
?>