簡體   English   中英

包括單獨的 html 文件不加載

[英]Including separate html file does not load

我正在嘗試使用 jquery 在我當前的 html 文件中簡單地加載另一個 html 文件,但沒有顯示任何內容。 我嘗試按照此處的答案之一進行操作,但仍然沒有看到我的 html 文件中出現任何內容。

關於.html

<!DOCTYPE html>
<html>

  <head>
    <script src="http://code.jquery.com/jquery-2.1.4.js">
    $(function(){
      $("#topbar").load("b.html");
    });
    </script>
  </head>

  <body>
    <div id="topbar"></div>
  </body>
</html>

b.html

<p> This is the included file </p>

我在這里做錯了什么?

JS <script> 101:您可以在<script>...</script>塊中擁有一個完全定義的腳本,或者您可以從外部src加載腳本。 您不能在一個塊中同時擁有兩者。 如果您指定src ,則<script>塊的主體將被忽略。 你應該有

<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script> <-------------------------------------------^^^^^^^^^
$(function(){
  $("#topbar").load("b.html");
});
</script>

注意</script><script>添加。

試試這個:

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript">
$(function(){
    $.ajax({
      method: "GET",
      url: "b.html",
      dataType: 'html',
      data: { },
      success: function(data) {
          $("#topbar").html(data);
      }
    })      
});
</script>
</head>

<body>
<div id="topbar"></div>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM