在HTML页面中引入外部HTML文件的解决方案

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

在HTML页面中引入外部HTML文件的解决方案

在普通的静态html开发过程中,没必要用框架,只想用最基本的方式写几个静态页面出来,但是HTML中没有include语法,每个页面的公共部分都要手动复制粘贴一次,实在不科学……

在网上看了有如下的解决方案: ( 推荐学习:html教程 )

方案一:将html文件转为js文件,然后在页面加载的时候将其加载进来执行渲染

方案二:使用iframe标签进行引用

方案三:使用gulp插件gulp-html-import

本人推荐使用第三种方案,使用起来也很方便,下面介绍使用步骤:

1、npm install gulp -D

2、npm install gulp-html-import -D

3、目录结构:

    |    -- html-import    |   |    |   -- components    |   |    |    |   |    -- header.html    |   |    |    |   |    -- footer.html    |   |    |   -- index.html    |   -- gulpfile.js
登录后复制

4、gulpfile.js

   var gulp = require('gulp');   var htmlImport = require('gulp-html-import');      gulp.task('import', function () {       gulp.src('./*.html')          .pipe(htmlImport('./components/'))          .pipe(gulp.dest('dist'));    })
登录后复制

5、index.html

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>Gulp-html-import Example</title></head><body>    @import "header.html"    <p>Hello World</p>    @import "footer.html"</body></html># 使用标签@import "XXX.html"引入公共页面
登录后复制

6、header.html

<!-- header.html --><h1>I am the header</h1>
登录后复制

8、gulp import 运行gulp将页面进行合并最终会生成dist目录

9、/dist/index.html

<!-- /dist/index.html --><!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>Gulp-html-import Example</title></head><body>    <h1>I am the header</h1>    <p>Hello World</p>    <h1>I am the footer</h1></body></html>
登录后复制

以上就是在HTML页面中引入外部HTML文件的解决方案的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:手机端怎样用rem+scss做适配
下一篇:angular学习之路由模块浅析

发表评论

关闭广告
关闭广告