2015-12-08 57 views
-1

這裏是我的腳本: page.js如何將腳本移動到頭標籤與JavaScript?

$(document).ready(function() 
{ 
    $.ajax 
    ({ 
     type: "GET", 
     url: "page/contenta.php", 
     dataType: "html", 
     cache: false, 
     success: function(response) 
     { 
      $("#main-content").html(response); 
     } 
    }); 

    $("#contentb").live('click',function() 
    { 
     $.ajax 
     ({ 
      type: "GET", 
      url: "page/contentb.php", 
      dataType: "html", 
      cache: false, 
      success: function(response) 
      { 
       $("#main-content").html(response); 
      } 
     }); 

     return false; 
    }); 
}); 

的index.php

<html> 
<head> 
javascript1.js (ex.) 
javascript1.js (ex.) 
javascript1.js (ex.) 
</head> 
<body> 
<div id="main-content"></div> 
<a href="#" id="contentb">CLICK</a> 
</body> 
</html> 

contenta.php

<script type="text/javascript" src="js/javascripta.js"></script> 
<div class="content"></div> 

contentb.php

<script type="text/javascript" src="js/javascriptb.js"></script> 
<div class="content"></div> 

是否可以將腳本從contenta.php/contentb.php移動到index.php的標籤中? 例如結果:

<html> 
<head> 
javascript1.js (ex.) 
javascript1.js (ex.) 
javascript1.js (ex.) 
<script type="text/javascript" src="js/javascripta.js"></script> 
</head> 
<body> 
<div id="main-content">...Content A Loaded...</div> 
<a href="#" id="contentb">CLICK</a> 
</body> 
</html> 

和呼叫contentb.php後:

<html> 
<head> 
javascript1.js (ex.) 
javascript1.js (ex.) 
javascript1.js (ex.) 
<script type="text/javascript" src="js/javascriptb.js"></script> 
</head> 
<body> 
<div id="main-content">...Content B Loaded...</div> 
<a href="#" id="contentb">CLICK</a> 
</body> 
</html> 

如何將腳本放到頭標記使用javascript?

+1

,它已經被執行。 –

+0

是的,可以先移動腳本然後執行? – Schreiner

+0

你應該解釋你爲什麼要這樣做。這可能是更好的方法。 –

回答

0

聽起來可能與jQuery如何用腳本標記檢索HTML有關。看看this question,因爲它聽起來與您的問題類似。

由您移動它時加載,你可以嘗試將其追加到頭在你的成功方法,如下面的內容後插入腳本...

$("#contentb").live('click',function() 
{ 
    $.ajax 
    ({ 
     type: "GET", 
     url: "page/contentb.php", 
     dataType: "html", 
     cache: false, 
     success: function(response) 
     { 
      $("#main-content").html(response); 
      $("head").append('<script type="text/javascript" src="js/javascriptb.js"></script>'); 
     } 
    }); 

    return false; 
}); 
+0

對不起,但腳本無法移動。 – Schreiner

相關問題