2010-10-15 47 views
0

我需要動態更新內容每隔幾秒鐘,而不重新加載頁面,所以我認爲使用jquery load()函數,這加載內容很好,但不是從頭標記的js或jquery文件。jQuery和js文件未加載?

下面是示例代碼:

<!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>{$PAGE_TITLE}</title> 
      <link rel="stylesheet" type="text/css" href="../web/css/style.css" /> 
      <script type="text/javascript" src="../web/js/jquery-1.3.2.min.js" ></script> 
      <script type="text/javascript" src="../web/js/wz_tooltip.js"></script> 
      <script type="text/javascript" src="../web/js/tip_centerwindow.js"></script> 
      <script type="text/javascript" src="../web/js/tip_balloon.js"></script> 
      <script type="text/javascript"> 
      {literal} 
       $(document).ready(function() { 
        setInterval(function(){ 
         $("#content").load("load_contents.php"); 
        },20000); 
       ...... 
       }); 
      {/literal} 
      </script> 
     </head> 
     <body> 
      <div class="content"> 

      </div> 
     </body> 
    </html> 

推薦我的解決方案...

更新:工具提示的js沒有加載,Y ...

+2

所以...你想「重新加載」jquery文件?這似乎沒有必要... – 2010-10-15 12:22:26

+0

這可以幫助http://stackoverflow.com/questions/889967/jquery-load-call-doesnt-execute-javascript-in-loaded-html-file – 2010-10-15 12:23:41

+3

你的17個問題中的16個已被回答,但你只接受了其中4個答案。修復。 – 2010-10-15 12:29:52

回答

0

我找不到引用,但是加載的頁面中的任何JavaScript都將被自動禁用以防止衝突。如果您需要對加載的內容執行某些操作,我建議您在主頁上使用live()delegate()來處理加載的內容。

+0

可以給我的樣本,請........ – Murugesh 2010-10-15 12:52:33

+0

http://stackoverflow.com/questions/75943/how-do-you-execute-a-dynamically-loaded-javascript-block,如果你需要一個例子,它取決於你對加載的內容做什麼。 – Mottie 2010-10-15 12:56:06

0

如果可能的話,我同意關於使用實況(代理不是1.3.2中的選項)的fudgey。如果你的插件不支持這個(雖然我猜它不會工作),但是你可以傳遞一個函數給.load,當內容被加載時它會運行。在這裏你會想把這些新元素連接到你的插件。例如,所以例如說你想在裏面內容的所有div使用拖動從jQuery UI的,你可能會做

$(document).ready(function() { 
     var init = function(){ 
      $("#content div").draggable(); 
     }; 
     init(); 
     setInterval(function(){ 
      $("#content").load("load_contents.php", init) 
     },20000);     
}); 

如果你這樣做你的工具提示插件和其他任何你正在重新加載到內容,它應該管用。請小心只在#content內的內容上調用init方法,以便最終不會將工具提示兩次添加到其他區域。使用使用委託(升級到最新的jQuery)或活動的插件可避免每次添加元素時都需要執行選擇和重新綁定,從而加快重新加載速度。