2016-11-22 129 views
-1

我比較新,所以我可能沒有做到他們的權利,但我已經嘗試過。我得到的最接近的是我包含的代碼。這是一段時間的工作,但模式不會「開火」。我試圖補救,並失去了我的包容。所以在這裏。如何將html文件包含到另一個文件中?

HTML

<!--============================================--> 
<!-- End of Main Program, Modals Follow --> 
<!--============================================--> 
<section id="activeModal"> 
    <!--> placeholder for every modal in this project </!--> 
    <!--> each modal is stored in a separate file (modals.html) </!--> 
    <!--> main.js builds an include with a js script, appends it here, and it executes (hopefully) </!--> 
</section><!-- /activeModal --> 

JS

// event listener for modals 

$("button").on('click', function() { 
    for (i=0; i<this.attributes.length ;i++) {      // this - identifies the modal sought 
     if (this.attributes[i].name === "data-target") { 
      var target = this.attributes[i].value;     // get the modal to "fire" (data-target attribute) 
     }; 
    }; 
    modalId = "modals/"+target.substring(1, target.length)+".html";      // strip the '#' from target 
    $("#activeModal").empty();              // remove any previous modal information 
    includeHTML = '<!--#include virtual="'+modalId+'" -->';   // include via ssi (server side include) 
    $("#activeModal").append(includeHTML);        // append the 'import' html 
    $(target).modal("show");              // show the modal 
}); 

我期待的了約20情態動詞的HTML出現在activeModal的index.html<section>。然後,我有讓他們「火」的問題。

回答

1

基於這一行,看起來您嘗試在單擊按鈕時在客戶端使用服務器端包含。

includeHTML = '<!--#include virtual="'+modalId+'" -->';   // include via ssi (server side include) 

這是非常不可能的。

您無法用客戶端代碼觸發服務器端包含。服務器端包含必須在響應發送給客戶端之前完成。要從服務器獲取更多數據,您必須通過AJAX啓動另一個請求。

+0

新人的錯誤,但我認爲你是對的。 – doug5solas

0
var stackoverflowResponse = document.createElement("div"); 
stackoverflowResponse.innerHTML = "Look in the console for the answear"; 
document.body.replacewith(stackoverflowResponse); 
0

你可以試着改變你的index.html擴展至的.shtml,然後用Server Side Includes包含另一個文件的內容。

<!--#include file="included.html" --> 

當我這樣做,我也相信我不得不更新.htaccess文件來識別的.shtml作爲一個索引文件:

AddType text/html .shtml 
AddHandler server-parsed .shtml 
AddHandler server-parsed .shtml .html 
AddHandler server-parsed .shtml .htm 
0

那麼你可以嘗試這樣的使用jquery :

<html> 
    <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){ 
     $("#includedContent").load("xyz.html"); 
    }); 
    </script> 
    </head> 

    <body> 
    <div id="includedContent"></div> 
    </body> 
</html> 
+0

我想我嘗試了這種嘗試。我會再試一次。謝謝。 – doug5solas

相關問題