html代码复用各种方法

1.gulp-file-include(工具)

使用步骤:1.安装gulp以及gulp-file-include(NodeJs上)

2.新建gulpfile.js,配置 gulp-file-include:

var gulp =require('gulp');//引入gulp

var fileinclude  =require('gulp-file-include');//引入gulp-file-include

gulp.task('fileinclude', function() {

    gulp.src('src/**.html') .pipe(fileinclude({//gulp.src中存放要编译的文件

           prefix:'@@',

           basepath:'@file'

    })).pipe(gulp.dest('dist'));//gulp.dest中存放编译后的文件的存放地址

});

3.通过@@include('include/header.html')引用header.html

4.在命令行工具里,执行gulp fileinclude。执行完毕后,dist目录里就有相对应的html文件。

2.gulp-ejs(模板)

使用步骤:1.安装gulp-ejs(NodeJs上)

2.新建gulpfile.js,配置 gulp-file-include:

var gulp = require('gulp');//引入gulp

var ejs  = require('gulp-ejs');//引入gulp-ejs

gulp.task('ejs', function() {

     gulp.src('Views/Business/financeManage1.ejs')//gulp.src中存放要编译的文件

         .pipe(ejs({},{ext: '.html'}))//设置生成的文件后缀名为html

        .pipe(gulp.dest('Views/dist'));//gulp.dest中存放编译后的文件的存放地址

});

3.通过<%-include ../template.html  %>引用template.html

4.在命令行工具里,执行gulp ejs。执行完毕后,dist目录里就有相对应的html文件。

3.iframe

1.将项目放于本地服务器如xampp下

2.<iframe src="../template.html" width="100%" onload="reinitIframeEND()"></iframe>

3.function reinitIframe(){

        var iframe = document.getElementById("iframepage");

        console.log(iframe);

        try{

            var bHeight = iframe.contentWindow.document.body.scrollHeight;

            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

            var height = Math.max(bHeight, dHeight);

            iframe.height = height;

        }catch (ex){

            console.log(ex);

        }

}

var timer1 = window.setInterval("reinitIframe()", 500); //定时开始

function reinitIframeEND(){

        var iframe = document.getElementById("iframepage");

        console.log("3");

        try{

            var bHeight = iframe.contentWindow.document.body.scrollHeight;

            var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

            var height = Math.max(bHeight, dHeight);

            console.log(bHeight+":"+dHeight);

            iframe.height = height;

      }catch (ex){

            console.log(ex);

     }

     // 停止定时

     window.clearInterval(timer1);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 自适应高度,其实就是设置iframe的高度,使其等于内嵌网页的高度,从而看不出来滚动条和嵌套痕迹。对于用户体验和网...
    布拉德皮蛋_qzy阅读 8,664评论 0 3
  • Window和document对象的区别 window对象window对象表示浏览器中打开的窗口window对象是...
    FConfidence阅读 6,727评论 0 5
  • 前言 本文默认你已经安装好node环境,并且熟悉node命令,和window cd命令。 gulp简介 基于nod...
    9I阅读 5,969评论 4 50
  • 对网站资源进行优化,并使用不同浏览器测试并不是网站设计过程中最有意思的部分,但是这个过程中的很多重复的任务能够使用...
    懵逼js阅读 4,719评论 0 8
  • 不知不觉在书写群呆了快一个月了,中间有过冲突,有过诉说,也有无尽的包容、接纳和安抚,我们都是小心翼翼地活在当下;活...
    疲惫的快乐阅读 1,189评论 0 0