html⾥⾯怎么引别的页⾯_html中引⼊调⽤另⼀个公⽤html模板
⽂件的⽅法
查了⼀下资料,发现html中引⼊调⽤另⼀个html的⽅法有很多种,我都尝试了⼀下,就把他们都列出来吧:
其中推荐第⼀种和第六种,因为代码太长就写在最后了。其他的⽅法,可以⾃⼰尝试,看是不是适合你当前项⽬。
⼀、需要借助 jquery div+$(“#page1”).load(“b.html”) 。
参考代码:
$("#page1").load("page/Page_1.html");
$("#page2").load("page/Page_2.html");
⼆、iframe 会⽣成⼀个边框,需要重新设置⼀下样式,相当于在页⾯内嵌⼊了⼀个页⾯。
参考代码:
三、object引⼊ 同样会⽣成⼀个边框,需要重新设置⼀下样式。
参考代码:
四、import引⼊ 这个我并没有试验成功,可能是我打开⽅式不对。
参考代码:
console.log(page1.import.body.innerHTML);
五、bootstrap的panel组件,或者easyui的window组件,有点类似这个效果;
六、通过⼀个 include.js 控制引⼊⽂件。
1、将下⽅js⽂件代码保存成 include.js ⽂件引⼊;
2、在页⾯中通过  载⼊模板⽂件。
参考代码:
js⽂件代码:(function(window, document, undefined) {
var Include39485748323 = function() {}
Include39485748323.prototype = {
//倒序循环
forEach: function(array, callback) {
var size = array.length;
for(var i = size - 1; i >= 0; i--){
callback.apply(array[i], [i]);
}
},
getFilePath: function() {
var curWwwPath=window.document.location.href;
var pathName=window.document.location.pathname;
var localhostPaht=curWwwPath.substring(0,curWwwPath.indexOf(pathName)); var projectName=pathName.substring(0,pathName.substr(1).lastIndexOf('/')+1); return localhostPaht+projectName;
},
//获取⽂件内容
getFileContent: function(url) {
var ie = navigator.userAgent.indexOf('MSIE') > 0;
var o = ie ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); o.open('get', url, false);
o.send(null);
sponseText;
},
parseNode: function(content) {
var objE = ateElement("div");
objE.innerHTML = content;
return objE.childNodes;
},
executeScript: function(content) {
var mac = /
var r = "";
while(r = (content)) {
eval(r[1]);
}
},
getHtml: function(content) {
var mac = /
return content;
},
getPrevCount: function(src) {
var mac = /\.\.\//g;
var count = 0;
html document是什么(src)) {
count++;
}
return count;
},
getRequestUrl: function(filePath, src) {
if(/http:\/\//g.test(src)){ return src; }
var prevCount = PrevCount(src);
while(prevCount--) {
filePath = filePath.substring(0,filePath.substr(1).lastIndexOf('/')+1); }
return filePath + "/"+place(/\.\.\//g, "");
},
replaceIncludeElements: function() {
var $this = this;
var filePath = $FilePath();
var includeTals = ElementsByTagName("include");
this.forEach(includeTals, function() {
//拿到路径
var src = Attribute("src");
//拿到⽂件内容
var content = $FileContent($RequestUrl(filePath, src)); //将⽂本转换成节点
var parent = this.parentNode;
var includeNodes = $this.parseNode($Html(content));
var size = includeNodes.length;
for(var i = 0; i < size; i++) {
parent.insertBefore(includeNodes[0], this);
}
//执⾏⽂本中的额javascript
$uteScript(content);
//替换元素 placeChild(includeNodes[1], this);
})
}
}
new Include39485748323().replaceIncludeElements(); }
})(window, document)
参考:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。