jquery load()相容ff用法

來源:互聯網
上載者:User

jquery load()用法

$('#result').load('ajax/test.html', function() {
  alert('load was performed.');
});

調用load方法的完整格式是:load( url, [data], [callback] ),
其中:url:是指要匯入檔案的地址。
data:選擇性參數;因為load不僅僅可以匯入靜態html檔案,還可以匯入動態指令碼,例如php教程教程檔案,所以要匯入的是動態檔案時,我們可以把要傳遞的參數放在這裡。
callback:選擇性參數;是指調用load方法並得到伺服器響應後,再執行的另外一個函數。
 

$('#result').load('ajax/test.html #container');

一:如何使用data

1.載入一個php檔案,該php檔案不含傳遞參數$("#myid").load("test.php");

//在id為#myid的元素裡匯入test.php運行後的結果
2. 載入一個php檔案,該php檔案含有一個傳遞參數

$("#myid").load("test.php",{"name" : "adam"});

//匯入的php檔案含有一個傳遞參數,類似於:test.php?name=adam
3. 載入一個php檔案,該php檔案含有多個傳遞參數。註:參數間用逗號分隔

$("#myid").load("test.php",{"name" : "adam" ,"site":"61dh.com"});

//匯入的php檔案含有一個傳遞參數,類似於:test.php?name=adam&site=61dh.com
4. 載入一個php檔案,該php檔案以數組作為傳遞參數

$("#myid").load("test.php",{'myinfo[]', ["adam", "61dh.com"]});

//匯入的php檔案含有一個數組傳遞參數。
注意:使用load,這些參數是以post的方式傳遞的,因此在test.php裡,不能用get來擷取參數。

二:如何使用callback

比如我們要在load方法得到伺服器響應後,慢慢地顯示載入的內容,就可以使用callback函數。代碼如下:

$("#go").click(function(){

$("#myid").load("welcome.php", {"lname" : "cai", "fname" : "adam", function(){

$("#myid").fadein('slow');}

);

});


 

備忘:

在load的url裡加上空格後面就可以跟選取器了。

例如:

$("body").load("test.html #a");

<!doctype html>  <html>  <head>    <style>   body{ font-size: 12px; font-family: arial; }   </style>    <script src="http://code.jquery.com/jquery-1.5.js"></script>  </head>  <body>      <b>footer navigation:</b>  <ol id="new-nav"></ol>    <script>    $("#new-nav").load("/ #jq-footernavigation li");  </script>    </body>  </html>

執行個體二

<!doctype html>  <html>  <head>    <style>    body{ font-size: 12px; font-family: arial; }    </style>    <script src="http://code.jquery.com/jquery-1.5.js"></script>  </head>  <body>      <b>successful response (should be blank):</b>  <div id="success"></div>  <b>error response:</b>  <div id="error"></div>      <script>  $("#success").load("/not-here.php", function(response, status, xhr) {    if (status == "error") {      var msg = "sorry but there was an error: ";      $("#error").html(msg + xhr.status + " " + xhr.statustext);    }  });    </script>    </body>  </html>

注意:只有當在這個元素完全載入完之前綁定load的處理函數,才會在他載入完後觸發。如果之後再綁定就永遠不會觸發了。所以不要在$(document).ready()裡綁定load事件,因為jquery會在所有dom載入完成後再綁定load事件。

再看一個load相容 ff做法


 

<html>
<head>
<script type="text/網頁特效" src="jquery-1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#b01").click(function(){
$('#mydiv').load('http://www.baidu.com');
});
});
</script>
</head>
<body>
<div id="mydiv"><h2>let ajax change this text</h2></div>
<button id="b01" type="button">change content</button>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.