2011-10-21 40 views
1

我在test.php文件的內容中加載了下面的代碼。 php文件中有很多代碼,因此需要一些時間來加載。當它加載時,「加載內容」文本仍然存在。我希望能夠: - 在頁面加載時顯示加載符號 - 內容出現後隱藏「加載內容」文本使用JQuery將內容加載到PHP文件中

我該怎麼做?

<script type="text/javascript"> 
      $(document).ready(function(){ 
      $("a").click(function(){ 
      $("#test").load('test.php'); 
      }); 
     }); 
    </script> 

<div id="test"></div> 
<a>Load content</a> 

UPDATE:

而且我想這是一個鏈接(因爲它目前不觸摸設備上工作) - 有什麼建議?

+0

要麼把'負載content'您的DIV中作爲OptimusCrime說或只是隱藏它:'$( 「A」)的hide();' – ComFreek

+0

首先得到[裝填圖像](http://ajaxload.info/)。 [顯示](http://api.jquery.com/show/)在調用'load'之前。然後,在['load'](http://api.jquery.com/load/)的回調函數中,[hide](http://api.jquery.com/hide/)'a'標籤。 –

+0

也許您需要將'href'屬性添加到'a'標籤? – Ragnis

回答

3

你既可以嵌套<div id="test">標籤所以​​功能齊全或添加代碼隱藏,當它被刪除裏面的<a>標籤/刪除<a>標記爲AJAX調用回調函數:

<div id="test"><a>Load content</a></div> 

或者在你的JavaScript

$("a").click(function(){ 
    /*Code to show loading message*/ 
    $("#test").load('test.php', function() { 
     //this is a callback function that will fire after the data from test.php is loaded into the test div 
     $('a').hide();//or you can use .remove() to completely remove the tag 
     /*Code to remove the loading message*/ 
    }); 
}); 
1

將負載內容放在div中。完成後,負載將覆蓋此項。

0

切換到$ .ajax()。 $ .load和$ .get是輔助程序快捷方式,可用於主.ajax()方法,該方法允許有更多配置選項。

在beforeSend回調中設置加載器動畫,並在onComplete回調中將其刪除(無論pull是成功還是錯誤,此回調都會運行)。相應地,您可能希望在成功/錯誤回調中執行其他操作。

http://api.jquery.com/jQuery.ajax/

+0

老實說,這沒有回答這個問題。 'load()'顯然是「足夠好」,因爲他正在做的事情,並且使用'ajax()'不會幫助他使「加載內容」消失(基於他的當前標記)。 – riwalk

0

你需要在你的PHP腳本中使用「print」。

當我這樣做,我使用jquery AJAX,並顯示在onSuccess

1

響應請參閱the jQuery documentation for .load()。這裏是你如何能做到這一點:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("a").click(function(){ 
      // Display a loading message: 
      $("#test").html('<p>Loading...</p>'); 

      // Load our data: 
      $("#test").load('test.php', function() { 
       // Code here runs when the load() completes: 
       $("a").hide(); 
      }); 
     }); 
    }); </script> 

<div id="test"></div> <a>Load content</a> 
相關問題