濮阳杆衣贸易有限公司

主頁 > 知識庫 > 多個HTML頁面共同調(diào)用一段html代碼的方法

多個HTML頁面共同調(diào)用一段html代碼的方法

熱門標(biāo)簽:江西ai電銷機(jī)器人如何 地圖標(biāo)注沿海城市房價 高德地圖標(biāo)注廁所 智能語音電銷機(jī)器人客戶端 西安金倫外呼系統(tǒng) 中國地圖標(biāo)注城市的 威海語音外呼系統(tǒng)平臺 通遼地圖標(biāo)注app 地圖標(biāo)注員工作內(nèi)容

方法一、使用script方法:

制作一個共用頭部文件head.js或一個共用底部文件foot.js。如主頁文件是mac.htm,調(diào)用頭部或底部文件的方法是:在主頁文件代碼的開始位置和結(jié)束位置分別增加下面的代碼:<script src=’head.js’>和<script src=’foot.js’>調(diào)用共同的網(wǎng)頁頭部或者網(wǎng)頁底部,減少了每個頁面都要編寫頭部或底部的復(fù)雜程度,而且方便修改,只要修改一個頭部或者底部文件,所有頁面的頭部或者底部都隨之改變,增加了工作效率。

導(dǎo)航條HTML實現(xiàn)代碼如head.html:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Examples</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link rel="stylesheet" type="text/css" href="../css/head.css">
</head>

<body>
    <div class='miaov_head'>
        <ul>
            <li><a href="http://www.cnblogs.com/jtjds/">Mac</a></li>
            <li><a href="http://www.cnblogs.com/jtjds/">iPad</a></li>
            <li><a href="http://www.cnblogs.com/jtjds/">iPhone</a></li>
            <li><a href="http://www.cnblogs.com/jtjds/">Watch</a></li>
            <li><a href="http://www.cnblogs.com/jtjds/">Music</a></li>
            <li><a href="http://www.cnblogs.com/jtjds/">Contact Us</a></li>
        </ul>
    </div>
</body>

</html>

其css文件為head.css:

* {
    margin: 0;
    padding: 0;
}

body {
    background: white;
    position: relative;
    height: 100%;
    color: #777;
    font-size: 13px;
}


li {
    list-style: none;
    text-decoration: none;
}

.miaov_head {
    height: 36px;
    width: 100%;
    margin: 0 auto;
    background: black;
    margin-bottom: 0px;
}


.miaov_head ul {
    float: left;
    width: 900px;
    height: 36px;
    margin-top: 0px;
    color: white;
    position: absolute;
    top: 0px;
    margin-left: 250px;
}

.miaov_head ul li {
    float: left;
    padding-left: 80px;
    margin-left: 0px;
    color: white;
    list-style: none;
}

.miaov_head ul li a {
    color: white;
    font-size: 14px;
    text-decoration: none;
}

.miaov_head input {
    position: absolute;
    top: 5px;
    margin-left: 1000px;
    width: 200px;
    height: 22px;
}

.miaov_head a {
    line-height: 36px;
    color: #777;
}

.miaov_head a:hover {
    color: #555;
}

將以上HTML代碼轉(zhuǎn)換為JavaScript:

document.writeln("<!DOCTYPE html>");
document.writeln("<html>");
document.writeln("<head>");
document.writeln("<meta charset=\&;utf-8\&;>");
document.writeln("<meta http-equiv=\&;X-UA-Compatible\&; content=\&;IE=edge,chrome=1\&;>");
document.writeln("<title>Examples</title>");
document.writeln("<meta name='description' content=\&;\&;>");
document.writeln("<meta name='keywords' content=\&;\&;>");
document.writeln("<link rel=\&;stylesheet\&; type=\&;text/css\&; href=\&;../css/head.css\&;>");
document.writeln("</head>");
document.writeln("<body >");
document.writeln(" <div class=\'miaov_head'>");
document.writeln("    <ul>");
document.writeln("      <li><a href=\&;http://www.cnblogs.com/jtjds/\&;>Mac</a></li>");
document.writeln("      <li><a href=\&;http://www.cnblogs.com/jtjds/\&;>iPad</a></li>");
document.writeln("      <li><a href=\&;http://www.cnblogs.com/jtjds/\&;>iPhone</a></li>");
document.writeln("      <li><a href=\&;http://www.cnblogs.com/jtjds/\&;>Watch</a></li>");
document.writeln("      <li><a href=\&;http://www.cnblogs.com/jtjds/\&;>Music</a></li>");
document.writeln("      <li><a href=\&;http://www.cnblogs.com/jtjds/\&;>Contact Us</a></li>");
document.writeln("    </ul>");
document.writeln("</div>");
document.writeln(" ");
document.writeln("</body>");
document.writeln("</html>");
document.writeln("");

并保存在head.js中,保存之后當(dāng)需要使用它的時候,可在頭部調(diào)用js文件,例如在mac.html中調(diào)用head.js:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Examples</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <script type="text/javascript" src="../javascript/head.js"></script>
</head>

<body>
    <ul>
        <li><a href="http://www.cnblogs.com/jtjds/">Mac</a></li>
        <li><a href="http://www.cnblogs.com/jtjds/">iPad</a></li>
        <li><a href="http://www.cnblogs.com/jtjds/">iPhone</a></li>
        <li><a href="http://www.cnblogs.com/jtjds/">Watch</a></li>
        <li><a href="http://www.cnblogs.com/jtjds/">Music</a></li>
        <li><a href="http://www.cnblogs.com/jtjds/">Contact Us</a></li>
    </ul>
</body>

</html>

在瀏覽器中查看:

 

方法二、使用$("selector").load()

為了避免多頁面情形下的代碼重復(fù),可以利用 load() 方法,將重復(fù)的部分(例如導(dǎo)航欄)放入單獨的文件

//1.當(dāng)前文件中要插入的地方使用此結(jié)構(gòu):
<div class="include" file="***.html"></div>

//2.***.html中放入內(nèi)容,用html格式僅僅因為會有編輯器的書寫輔助。。

//3.代碼:
$(".include").each(function() {
    if (!!$(this).attr("file")) {
        var $includeObj = $(this);
        $(this).load($(this).attr("file"), function(html) {
            $includeObj.after(html).remove(); //加載的文件內(nèi)容寫入到當(dāng)前標(biāo)簽后面并移除當(dāng)前標(biāo)簽
        })
    }
});

或者在index文件里只寫重復(fù)部分,剩下的一股腦放各自單獨文件 load() 進(jìn)來~

相比于第一種,個人更推薦第二種方法。

參考:jQuery - AJAX load() 方法

到此這篇關(guān)于多個HTML頁面共同調(diào)用一段html代碼的方法的文章就介紹到這了,更多相關(guān)HTML共同調(diào)用一段html代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:營口 眉山 晉中 崇左 北海 河池 阜陽 青海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《多個HTML頁面共同調(diào)用一段html代碼的方法》,本文關(guān)鍵詞  多個,HTML,頁面,共同,調(diào)用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《多個HTML頁面共同調(diào)用一段html代碼的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于多個HTML頁面共同調(diào)用一段html代碼的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    桃园市| 旬阳县| 湘乡市| 南乐县| 全南县| 九龙坡区| 章丘市| 贵定县| 闵行区| 腾冲县| 喀什市| 望谟县| 鸡东县| 涪陵区| 响水县| 红原县| 洛宁县| 甘谷县| 博爱县| 新巴尔虎左旗| 云浮市| 阜宁县| 格尔木市| 卢氏县| 呼图壁县| 葵青区| 五原县| 东乡县| 重庆市| 兴山县| 香河县| 章丘市| 行唐县| 磴口县| 湄潭县| 治多县| 兴文县| 承德市| 沛县| 神农架林区| 青海省|