簡體   English   中英

如何將一個html文件的內容加載到另一個html文件及其所有腳本和樣式?

[英]how to load content of one html file into another html file with all its scripts and styles?

我知道這個問題在這里被多次詢問並且我已經閱讀了這些答案,但我的問題比這更長。 假設有兩個文件:a.html和b.html,現在:

  • a.html包含整個項目中常見的所有腳本和樣式以及內容的<div>標記。

  • b.html僅包含加載a.html文件的div標記所需的代碼。

  • 還有一些僅適用於b.html的其他腳本和樣式。

    那么,我如何將頁面特定的js和css與頁面的內容加載到a.html的div中? 以及當我用其自己的js和css文件調用另一個c.html文件的內容時如何刪除它們? 任何建議或提示將不勝感激。 謝謝。

使用Jquery Ajax將第2頁加載到div中(如下例所示)

第1頁(HTML)

<div id="loadepage2here" ></div>
<div id="loadepage3here" ></div>

第1頁(js)

$(document).ready(function()
{
  $.get('/locationOfPage2.html',function(data){
     $("#loadepage3here").empty();
     $("#loadepage2here").html(data);
   });
 $.get('/locationOfPage3.html',function(data){
     $("#loadepage2here").empty();
     $("#loadepage3here").html(data);
   });
});

編輯:詢問如何只使用div為多頁

第1頁(HTML)

<div id="loadepagehere" ></div>
<button onClick="loadPage('page1.html')" >Page 1</button >
<button  onClick="loadPage('page2.html')" >Page 2</button >

第1頁(js)

loadPage(location)
{
    $.get(location,function(data){
       $("#loadepagehere").empty();
       $("#loadepagehere").html(data);
     });
}

您可以使用angular.js來實現此目的。 下面是實現此目的的代碼段。

    var dynamicHTML = $('#divLoadHTML');
    dynamicHTML.load('/Pages/Common/b.html')
    $compile(dynamicHTML.contents())($scope);

divHTML是a.html頁面上的div。

暫無
暫無

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

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