簡體   English   中英

我可以避免在jquery中重復此代碼嗎?

[英]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);

htmlcss定義了哪些變量,如下所示(每行末尾有一個反斜杠):

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM