Javascript 如何使用jQuery添加公共页眉页脚

Javascript 如何使用jQuery添加公共页眉页脚,javascript,jquery,Javascript,Jquery,我正在尝试使用jQuery创建公共页眉页脚。我使用这个函数,但它并没有从header.html加载内容,而是在控制台中显示错误(参见图中) $(函数(){ $(“[数据加载]”)。每个(函数(){ $(this).load($(this).data(“load”),function(){ }); }); }) 这是我的整个html页面标记 <!DOCTYPE html> <html> <head> <title></title&

我正在尝试使用jQuery创建公共页眉页脚。我使用这个函数,但它并没有从header.html加载内容,而是在控制台中显示错误(参见图中)


$(函数(){
$(“[数据加载]”)。每个(函数(){
$(this).load($(this).data(“load”),function(){
});
});
})
这是我的整个html页面标记

<!DOCTYPE html>
<html>
<head>
    <title></title>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

hello shubham

<div data-load="header.html"></div>


<script type="text/javascript">
    $(function(){
    $("[data-load]").each(function(){
        $(this).load($(this).data("load"), function(){
        });
    });
})
</script>

</body>


</html>

你好,shubham
$(函数(){
$(“[数据加载]”)。每个(函数(){
$(this).load($(this).data(“load”),function(){
});
});
})

您需要从服务器提供文件,在您的情况下可能是
localhost
,而不是简单地在浏览器中打开html文件

错误消息试图说明您尝试执行的操作不允许使用
文件
协议

最好设置本地服务器。您可以使用或轻松地完成此操作


如果尚未安装,请先安装

您应该使用localserver,您可以使用node.js命令提示符来实现这一点

first access your directory and execute the following command : 
http-server . -p 8000

then you can access using the url : http://127.0.0.1:8000
first access your directory and execute the following command : 
http-server . -p 8000

then you can access using the url : http://127.0.0.1:8000