是否有任何 jquery 插件可以在 HTML 页面中保留常见的页眉和页脚?

Is there any jquery plugin to keep common header and footer in a HTML page?(是否有任何 jquery 插件可以在 HTML 页面中保留常见的页眉和页脚?)
本文介绍了是否有任何 jquery 插件可以在 HTML 页面中保留常见的页眉和页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否有任何 jquery 插件可用于在 HTML 页面中保留公共页眉和页脚?

Is there any jquery pluginto use to keep common header and footer in a HTML page?

如果我可以将 header.html 和 footer.html 添加到 index.html 中.

Like if i can add header.html and footer.html into index.html.

我知道我可以使用 php,但如果可能的话,我不想在本地 PC 上安装任何服务器.稍后完成所有工作后,我将使用 php 包含

I know I can with php but if possible I want to do without installing any server on my local PC. later after all work done i will use php includes

header.html

<title>Template</title>
<meta name="description" content="">
<link rel="stylesheet" href="css/styles.css?v=1.0">
<script src="js/modernizr-2.0.6.min.js"></script>

index.html

{include header.html}

<body class="home">

{include footer.html}

Footer.html

<footer class="f1">
    <p>copyright &copy; year</p>
</footer><!-- .f1 -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="js/general.js"></script>
</body>
</html>

推荐答案

jQuery 本身有一些特性可以让你做到这一点.如果您可以在每个页面上选择页眉/页脚的容器,您可以插入任何您想要的通用内容,甚至替换现有的内容.

jQuery itself has some features that allow you to do that. If you can select header's/footer's container on each page, you can insert any common content you want, even replace the existing one.

你可以这样做:

jQuery(function(){
    jQuery('.header').html('SOME COMMON HEADER');
    jQuery('.footer').html('SOME COMMON FOOTER');
});

在此处查看实际操作:http://jsfiddle.net/6sCfm/

或者,如果你坚持加载外部文件,你可以这样做(使用 .load()):

Or, if you insist on loading external files, you can do this (using .load()):

jQuery(function(){
    jQuery('.header').load('header.html');
    jQuery('.footer').load('footer.html');
});

但请注意您作为参数提供的正确路径,并确保 文件应该只有您需要的 HTML(没有 <head><body> 标签等 - 它会使 HTML 不正确).

but pay attention to correct paths you are giving as parameters and make sure, that files should have only the HTML you need (no <head>, <body> tags, etc. - it will make HTML incorrect).

如果您有整个页面(使用 <head><header> 等),则只能加载其中的一部分.最好为您要加载的部分(例如 headerfooter)提供有意义的 ID,并在路径中提供它们作为选择器:

If you have whole pages (with <head>, <header> etc.), you can load only parts of them. Preferably give meaningful IDs to the parts you want to load (eg. header and footer) and supply them in the path, after space, as selector:

jQuery(function(){
    jQuery('.header').load('header.html #header');
    jQuery('.footer').load('footer.html #footer');
});

这应该足够灵活:)

这篇关于是否有任何 jquery 插件可以在 HTML 页面中保留常见的页眉和页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)