標籤:成功 htm ons 單元 dem onclick lte 遠程 hot
1.demo1
<script language="javascript" type="text/javascript">//通過這個函數來非同步擷取資訊function Ajax(){ var xmlHttpReq = null; //聲明一個Null 物件用來裝入XMLHttpRequest if (window.ActiveXObject){//IE5 IE6是以ActiveXObject的方式引入XMLHttpRequest的 xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest){//除IE5 IE6 以外的瀏覽器XMLHttpRequest是window的子物件 xmlHttpReq = new XMLHttpRequest();//執行個體化一個XMLHttpRequest } if(xmlHttpReq != null){ //如果對象執行個體化成功 xmlHttpReq.open("GET","test.php",true); //調用open()方法並採用非同步方式 xmlHttpReq.onreadystatechange=RequestCallBack; //設定回呼函數 xmlHttpReq.send(null); //因為使用get方式提交,所以可以使用null參調用 } function RequestCallBack(){//一旦readyState值改變,將會調用這個函數 if(xmlHttpReq.readyState == 4){ if(xmlHttpReq.status == 200){ //將xmlHttpReq.responseText的值賦給ID為 resText 的元素 document.getElementById("resText").innerHTML = xmlHttpReq.responseText; } } }}</script></head><body><input type="button" value="Ajax提交" onclick="Ajax();" /><div id="resText" ></div></body>
2.load()方法:能載入遠程HTML代碼並插入DOM中
<script language="javascript" type="text/javascript"> $(function(){ $("#send").click(function(){ $("#resText").load("test.html"); }) })</script></head><body><input type="button" id="send" value="Ajax擷取" /><div class="comment"> 已有評論:</div><div id="resText" ></div></body>
點擊前後結果如下
3.如果需要傳遞一些參數給伺服器的頁面,可以使用$get()或$post()
3.1 $get()
<!-- 引入jQuery --><script src="../scripts/jquery.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ $(function(){ $("#send").click(function(){ $.get("get1.php", { //發送給get1.php的資料 username : $("#username").val() , content : $("#content").val() }, function (data, textStatus){ $("#resText").html(data); // 回調,把返回的資料添加到頁面上,還可以採用XML、json格式 } ); }) })//]]></script></head><body><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="send" value="提交"/></p></form><div class=‘comment‘>已有評論:</div><div id="resText" ></div></body>
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>";?>
3.2 $post()
與$get()方法的結構和使用方式都相同,但也存在以下區別
a.參數跟的位置不同
b.GET對傳輸資料有限制,請求的資料會被緩衝,存在安全隱患
c.傳遞的資料在服務端擷取的方式不同,PHP中資料通過$_GET[],$_POST[]擷取,兩種方式都可通過$_REQUEST[]擷取
4 $.getScript()和$.getJson()--第三層
4.1 jQuery提供了$.getScript()方法直接載入js檔案,也可以添加回掉函數
4.2 $.getJson()載入json檔案
jQuery通用的遍曆方法$.each(),$.each()不同於each()方法,它是一個全域函數,不操作jQuery對象,而是以一個數組或對象作為第一個參數,以一個回呼函數作為第二個參數,回呼函數擁有兩個參數:第一個:對象的成員或數組的索引,第二個:對應變數或內容
1、選取器+遍曆$(‘div‘).each(function (i){ i就是索引值 this 表示擷取遍曆每一個dom對象});2、選取器+遍曆$(‘div‘).each(function (index,domEle){ index就是索引值 domEle 表示擷取遍曆每一個dom對象});3、更適用的遍曆方法1)先擷取某個集合對象2)遍曆集合對象的每一個元素var d=$("div");$.each(d,function (index,domEle){ d是要遍曆的集合 index就是索引值 domEle 表示擷取遍曆每一個dom對});
這裡屬於第三類:
<!-- 引入jQuery --><script src="../scripts/jquery.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[ $(function(){ $(‘#send‘).click(function() { $.getJSON(‘test.json‘, function(data) { $(‘#resText‘).empty(); var html = ‘‘; $.each( data , function(commentIndex, comment) {//jQuery通用的遍曆方法$.each() html += ‘<div class="comment"><h6>‘ + comment[‘username‘] + ‘:</h6><p class="para">‘ + comment[‘content‘] + ‘</p></div>‘; }) $(‘#resText‘).html(html); }) }) })//]]></script></head><body> <br/> <p> <input type="button" id="send" value="載入"/> </p><div class="comment">已有評論:</div> <div id="resText" > </div></body>
test.json
[ { "username": "張三", "content": "沙發." }, { "username": "李四", "content": "板凳." }, { "username": "王五", "content": "地板." }]
5 $.ajax()方法
它是jQuery最底層的Ajax實現
6 serialize()
jQuery提供一種簡化的方法來提交表單,不用一一列舉表單元素,而是直接使用serialize(),它能將DOM元素內容序列化為字串。
即使在表單中在增加欄位,指令碼仍能使用
</style> <!-- 引入jQuery --><script src="../scripts/jquery.js" type="text/javascript"></script><script type="text/javascript"> $(function(){ $("#send").click(function(){ $.get("get1.php", $("#form1").serialize() , function (data, textStatus){ $("#resText").html(data); // 把返回的資料添加到頁面上 } ); }) })</script></head><body><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="send" value="提交"/></p></form><div class=‘comment‘>已有評論:</div><div id="resText" ></div></body>
7 $.param()
<script type="text/javascript">//<![CDATA[ $(function(){ var obj={a:1,b:2,c:3}; var k = $.param(obj); alert(k) // 輸出 a=1&b=2&c=3 })//]]></script>
8 Ajax全域事件
Ajax請求開始時,會觸發ajaxStart()方法的回呼函數;
Ajax請求結束時,會觸發ajaxStop()方法的回呼函數;
<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>
【jQuery】jQuery與Ajax的應用