![](/img/trans.png)
[英]How to include a html file inside another html file with Vanilla Javascript?
[英]How do I include html file inside another html file
我知道這個問題有很多答案,但我已經嘗試了所有這些問題,但我無法解決這個問題。 我的具體問題是我有兩個文件, index.html
和part1.html
,我需要將part1.html
包含到index.html
。
我嘗試過使用:
.load() function of jquery.
<!--#include file="part1.shtml" -->
include with html like <!--#include file="part1.html" -->
但這解決了我的問題,為什么我需要呢? 我有一個包含大量代碼的頁面,在這一刻幾乎不可能工作。 為什么不使用服務器語言? 我這樣做,但我在R中使用一個名為shiny的框架,相信我的服務器語言不是解決方案xD。
當我使用.load()
出現錯誤XMLHttpRequest cannot load file:///home/... Origin null is not allowed by Access-Control-Allow-Origin
。
謝謝大家。
use jquery
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function{
$("#includedContent").load("b.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
.load
應該工作正常。 一個簡單的例子,說你有以下div:
<div id="contentDiv"></div>
使用jQuery,您可以將單獨的HTML文件加載到該div中:
$(document).ready(function() {
$("#contentDiv").load("part1.html");
});
這與SOP(同源策略)有關,最近我遇到了同樣的問題...所以我用過這個...希望你能這樣做......這里是鏈接: http : //jsfiddle.net/JKZ2h / 1 /我的HTML代碼:
<li><a href="#" class="about-link">About Us</a></li>
<div class="bdy"></div>
我的js代碼:
$('.about-link').click(function() {
$('.bdy').html('<iframe sandbox="allow-scripts allow-same-origin" style="width:100%;height:435px;" src="http://www.erail.in/?R=12622"><iframe>');
})
甚至這適用於靜態網頁:這里面的任何功能...工作..:
$('.bdy').html('<object style="width:100%;height:435px;"data="http://www.jhsoftech.com/contactus.php">');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.