2013-01-13 41 views
0

我有一個自動刷新按鈕,當按鈕開啓時,我的網頁的特定部分應該刷新。例如,如果我有一個DIV,當按鈕爲ON時,此DIV中的內容應該重新加載。如果該按鈕處於關閉狀態,則DIV應保持不變。如何刷新我的網頁的特定部分?

我只有當自動刷新按鈕在整個頁面的編碼是令人耳目一新。是否有可能刷新網頁的特定部分?

這裏是我的編碼: -

var int=self.setInterval(function(){refreshPage()},60000); 
$(document).ready(function() { 
    var hashTag = window.location.href.split('#'); 
    if (hashTag[1] == 'reload') { 
     $('#refresh').addClass('refresh-on').html(''); 
    } 
    $('#refresh').on('click', function() { 
     $(this).toggleClass('refresh-on'); 
     if ($(this).hasClass('refresh-on')) 
     $(this).html('Refresh On'); 
     else 
     $(this).html('Refresh Off'); 
    }); 
}); 

function refreshPage() { 
    if ($('#refresh').hasClass('refresh-on')) { 
     location.hash = 'reload'; 
     window.location.reload(); 
    } else 
    location.hash = ''; 
} 
+0

除I幀和圖像,如何能在瀏覽器重裝只是一個網頁的一部分?如果您有辦法在應用程序中執行此操作,則需要使用AJAX專門定義它。 – Barmar

+0

@Barmer:謝謝兄弟! –

回答

2

您可以使用簡單的jQuery的load()方法,如果你能夠保存在一個單獨的頁面被刷新內容。

放在「content.php」

​​

任何需要的時候可以調用這個方法,它可以動態加載被刷新的內容。

0

事實上,jquery的load()方法是用外部腳本更新分離內容的好方法...
下面是一個包含2個div的小html頁面的例子。
一個令人耳目一新每1秒,另外每3秒......
的div使用refreshDiv功能進行刷新。
的refreshDiv函數有3個參數:

  • div_id:這是你想刷新div的ID;
  • script_file:要啓動刷新DIV腳本; (腳本/目錄);
  • 數據:傳遞給腳本(PHP)由GET方法中的一些數據;

    <html> 
    <head> 
        <title>Stack OverFlow - 14302842</title> 
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
        <script type="text/javascript"> 
         function refreshDiv(div_id, script_file, data) { 
         var today = new Date(); 
         $("#"+div_id).load("scripts/"+script_file+"?_"+Math.random()+"&data="+data);   
         } 
    
         $(document).ready(function() { 
          refreshDiv('div1','ls.php','/tmp'); 
          refreshDiv('div2','ls.php','/home'); 
    
          setInterval(function() { 
           // Do something every 3 seconds 
           refreshDiv('div1','ls.php','/tmp'); 
          }, 3000); 
    
          setInterval(function() { 
          // Do something every 1 seconds 
          refreshDiv('div2','ls.php','/home'); 
          }, 1000); 
    
         }); 
        </script> 
    </head> 
    <body> 
        <div id="div1" style="color: #F00; border: 1px solid #F00; text-align: center;">Some Text</div> 
        <div id="div2" style="color: #00F; border: 1px solid #00F; text-align: center;">Some Text</div> 
    </body> 
    </html>