html中引入公用的功能性html(避免重复性代码)

<!DOCTYPE html>
<html>
<head>
    <title>自动引入其他HTML标签</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="zh-CN" />
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
</head>
<body>

<div class="include" file="header.html">需要引入的</div>

<div>自身部分</div>
<div class="include" file="footer.html">需要引入的</div>
<script type="text/javascript">
//三种不同方式引入页面,加载的文件内容写入到当前标签里面并移除当前标签<div class="include" ></div>
//基于ajax的get请求
$(".include").each(function() {
    var file = $(this).attr("file");
    if(!!file) {
        var thisObj = $(this);
        $.get(file, function(html) {
            thisObj.after(html).remove();
        });
    }
});
//基于iframe请求的实现是要使用jQuery伪造一个隐藏的iframe标签出来请求文件内容,获取后移除它
$(".include").each(function(){
    var file = $(this).attr("file");
    if(!!file) {
        var $includeObj = $(this);
        $("<iframe style='display:none'>").attr("src", file).attr("name", file).appendTo($("body")).load(function(){
            $includeObj.after($(window.frames[file].document).text()).remove();
            $(this).remove();
        });
    }
});
//使用jQuery的load方法
$(".include").each(function(){
    if(!!$(this).attr("file")) {
        var $includeObj = $(this);
        $(this).load($(this).attr("file"), function(html){
            $includeObj.after(html).remove();    //加载的文件内容写入到当前标签后面并移除当前标签
        })
    }
});
</script>
</body>
</html>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值