代码之家  ›  专栏  ›  技术社区  ›  thedeepfield

如何使用jQuery或HTML5在所有页面上复制页眉和页脚?

  •  2
  • thedeepfield  · 技术社区  · 13 年前

    我知道使用rails和django,您可以使用模板将页眉和页脚作为单独的文件编写一次,并将其包含在所有网页中。我想知道,像这样的东西只需要jQuery或html5就能实现吗?如果是,怎么做?

    <html>
        <head></head>
        <body>
            <navigation> include code from header.html here </navigation>
            ...
            some page specific content
            ...
            <footer>include code from footer.html here</footer>
        </body>
    </html>
    
    1 回复  |  直到 13 年前
        1
  •  4
  •   OneChillDude    13 年前

    是的,如果你使用jQuery的 .load() 作用我觉得它看起来像

    $('footer').load('/assets/myfooter.html');

    这样就可以了。

    Documentation available here

    由于“同源策略”,你唯一不能做的就是从另一个域获取html,我认为这是为了防止CSRF攻击和/或会话劫持。