我比较新,所以我可能没有做对,但我已经尝试过了。我得到的最接近的是我所包含的代码。它工作了一段时间,但模态不会“开火”。我试图解决这个问题而失去了包容性。所以这里。
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出现在<section>
的activeModal index.html
中。然后,我有让他们“火”的问题。
答案 0 :(得分:1)
基于此行,当单击按钮时,您似乎正在尝试在客户端使用服务器端包含。
includeHTML = '<!--#include virtual="'+modalId+'" -->'; // include via ssi (server side include)
这根本不可能。
您无法使用客户端代码触发服务器端包含。必须在将响应发送到客户端之前进行服务器端包含。要从服务器获取更多数据,您必须通过AJAX发起另一个请求。
答案 1 :(得分:0)
var stackoverflowResponse = document.createElement("div");
stackoverflowResponse.innerHTML = "Look in the console for the answear";
document.body.replacewith(stackoverflowResponse);
答案 2 :(得分: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
答案 3 :(得分:0)
你可以使用jquery尝试这样的东西:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includedContent").load("xyz.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>