[英]Can I avoid this code duplication in jquery?
我确实需要在网站上每个页面的底部显示固定位置的内容,就像这样。
<div class="footer-fix">
<p>This is its contents......</p>
</div>
.footer-fix {
background:#fff;
position: fixed;
bottom: 0;
z-index: 999;
width: 100%;
}
我的问题是我的网站上是否有很多页面。 那么,我是否需要在几乎每个网页中添加此代码,或者是否有其他方法可以在每个页面中都没有添加此代码呢?
希望有人可以帮助我。
谢谢。
演示: http : //jsfiddle.net/qz94zkmm/
首先,是的,您可以在jQuery中完成。 但是不推荐!
这是一个可以将html和css样式动态插入元素的函数:
function appendHtmlTo(where, htmlText, cssText) {
$(where).append(htmlText).append('<style text="text/css">'+cssText+'</style>');
}
并调用此函数:
appendHtmlTo('body', html, css);
html
和css
定义了哪些变量,如下所示(每行末尾有一个反斜杠):
var html = '\
<div class="footer-fix">\
<p>This is its contents......</p>\
</div>\
';
var css = '\
.footer-fix {\
background:pink;\
position: fixed;\
bottom: 0;\
z-index: 999;\
width: 100%;\
}\
';
准备好文档后,请务必调用此功能!
您可以使用PHP轻松实现,只需创建footer.php文件并在其中插入代码即可。然后只需调用include(“ / path / to / footer.php”); 在每一页的底部,其。
您可以使用html标签Ex:
<body>
<header class="col-lg-12" style="background-color:#444444; ">
//Here i have Header
</header>
<div class="container-fluid" style="padding:10px; height:600px;">
// Here goes my Body
</div>
<footer class="col-lg-12 text-center" style="background-color:#444444; height:50px;">
//Here Goes my Footer
</footer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.