欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HTML5公共页面如何提取作为公用代码?

程序员文章站 2022-12-01 20:02:27
在网站制作中有很多部分的代码是相同的,如footer部分,可以把这段代码提取出来,在别的页面调用。 index.html ...

在网站制作中有很多部分的代码是相同的,如footer部分,可以把这段代码提取出来,在别的页面调用。

index.html

<!-- 底部加载外部文件 -->  
    <footer class="footer">  
    </footer>  
    <script>  
        $('.footer').load('conment/foot.html',function(responseTxt,statusTxt,xhr){  
            console.log('responseTxt,statusTxt,xhr')  
            // console.log(responseTxt)  
            // console.log(statusTxt)  
            // console.log(xhr)  
        })  
    </script>  

foot.html

<html lang="en">  
<head>  
    <title>Document</title>  
    <link rel="stylesheet" href="css/foot.css"></link>  
</head>  
<body>  
    ...  
</body>  
</html>  

重点:

$('.footer').load()调用时必须在服务器环境中打开HTML文件才能成功,不能直接双击打开

打开后浏览器的路径应该是https://127.0.0.1:8020/index.html,

而不是file:///G:/html/index.html这样的路径,否则在大部分的浏览器上都无法使用。

再此建议使用HBuilder这款编辑器,在运行时会创建服务器环境,并且在局域网中在别的手机或电脑上预览。