2016-11-22 1 views
-1

私は比較的新しいので、私は正しいことをしていないかもしれませんが、私はそれらを試しました。私が得た最も近いものは私が含むコードです。それはしばらくの間働いていたが、モーダルは「発火しない」。私はそれを救済しようとし、私の包含を失った。だからここに行く。htmlファイルを別のファイルに含めるには?

// 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 
}); 

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は私がactiveModal index.html<section>に表示されるように、約20モーダルのいずれかのHTMLを期待しています。そして、私はそれらを「火」にする問題があります。

答えて

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

あなたは.SHTMLためにあなたのindex.htmlの拡張子を変更して、別のファイルの内容を含めるようにServer Side Includesを使用して試みることができます。

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

私はこれをしたときは、私も、私は、インデックスファイルとして.shtmlのを認識するように.htaccessファイルを更新しなければならなかったと信じて:

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

関連する問題