在纯HTML中有没有类似于ASP.NET的主/内容页面的创建方式?
我想要用HTML创建几个页面,但希望它们都看起来一样,只是某些内容不同。有没有办法做到这一点而不必创建非常相似的多个页面?
在纯HTML中有没有类似于ASP.NET的主/内容页面的创建方式?
我想要用HTML创建几个页面,但希望它们都看起来一样,只是某些内容不同。有没有办法做到这一点而不必创建非常相似的多个页面?
//wait until the dom is loaded
$(document).ready(function () {
//adds menu.html content into any "#menu" element
$('#menu').load('menu.html');
});
有些其他答案中提到的,iframe 应该谨慎使用和节约使用。http://rev.iew.me/help-moving-from-iframes
http://fsvieira.com/2013/06/11/iframes-bad-for-a-website/
有一个重复的问题和答案在这里:如何使用 HTML 创建主页?
我使用第三方C#表单应用程序解决了这个问题。
页眉和页脚不同 在所有其他页面插入密钥。 (###footer###) 使用表单应用程序替换文件内容。
footer.html
<h2>this place is footer.</h2>
default.html
<h1>Default page</h1>
bla bla bla
###footer###
结果 default.html
<h1>Default page</h1>
bla bla bla
<h2>this place is footer.</h2>
以下是源代码
List list = new List();
private void sourceBtn_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog(this);
if (result == DialogResult.OK)
{
sourceTxt.Text = openFileDialog1.FileName;
}
}
private void fileListSelect_Click(object sender, EventArgs e)
{
var result = openFileDialog2.ShowDialog(this);
if (result == DialogResult.OK)
{
fileList.Items.AddRange(openFileDialog2.FileNames);
}
}
private void addSourceBtn_Click(object sender, EventArgs e)
{
list.Add(new sourceKey() { filename = sourceTxt.Text, key = keyTxt.Text });
sourceTxt.Clear();
keyTxt.Clear();
sourceTxt.Focus();
sourceList.DataSource = null;
sourceList.DataSource = list;
}
private void ConvertBtn_Click(object sender, EventArgs e)
{
foreach (var filename in fileList.Items)
{
string text = File.ReadAllText(filename.ToString());
foreach (var item in sourceList.DataSource as List)
{
text = text.Replace(item.key, File.ReadAllText(item.filename));
}
File.WriteAllText(filename.ToString(), text);
}
infoLabel.Text = "Done";
}
@@include('./header.html')
<!-- Content -->
<section>
<h1>Hello world</h1>
</section>
@@include('./footer.html')
其中一个具备重复块选项的最佳方法是使用 Gulp.js 和一些包。Gulp 是一个流行的 JavaScript 工具包,可用于自动化和优化您的工作流程。
要使用它,请首先使用 yarn 或 npm 在您的项目中安装 gulp:
yarn init
安装 gulp-file-include 插件:
yarn add gulp gulp-file-include -D
touch gulpfile.js
如果您正在使用Windows,请改用以下命令:
type "gulpfile.js"
const gulp = require('gulp');
const fileinclude = require('gulp-file-include');
const paths = {
scripts: {
src: './',
dest: './build/'
}
};
async function includeHTML(){
return gulp.src([
'*.html',
'!header.html', // ignore
'!footer.html' // ignore
])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest(paths.scripts.dest));
}
暂时将函数设置为默认值:
exports.default = includeHTML;
在 index.html 中添加 include 标签:
@@include('./header.html')
<!-- Content -->
<section>
<h1>Hello world</h1>
</section>
@@include('./footer.html')
yarn gulp
构建文件夹将会创建,并且里面包含 index.html 文件。
完成 :)
好吧,作为一个丑陋的解决方案,可以尝试使用<iframe>
标签。它们可以将远程页面加载到您的网站中,因此您可以定义一个类似于这样的“主模板”:
...
<body>
<div id="content">
<iframe src="content1.html"></iframe>
...
content1.html
内,您可以直接编写内容,而无需主要布局。<iframe>
只是将链接的页面直接放入其内部,没有其他功能。在您的内容 HTML 文件中,只需编写内容即可。当您想更改内容时,请更改 <iframe>
的 src=
。我永远不会在我的网站上这样做。如果我必须编写需要大量页面的代码,我会使用 Python 编写。如果不需要,我会复制并粘贴几次。 - Blender
<!--#include file="myIncludeFile.html" -->
,您可以在其中添加标题、页脚等内容。 - Mikael Östberg