1、使用Ajax動態拉取填充
$(document).ready(function(){ $('.top').load('topic-top.html'); $('.footer').load('topic-footer.html'); });
$(document).ready(function(){ $.ajax(function(){ url: url, //這里是靜態頁的地址 type: "GET",//靜態頁用get方法,否則服務器會報405錯誤 success: function(data){ var result = $(data).find('另一個html頁面中指定的一部分'); $('本頁面的ele').html(result); } }) )
2、服務端shtml包含
3、自己寫代理(比如用node),請求文件的時候自動加上頭尾文件
4、使用iframe(不推薦)
總之:用ajax 就行了
需要注意的是: 1 ajax是異步加載,需要把async設置成false。 2 Chrome不支持本地Ajax請求,chrome不支持本地打開頁面,不能加載這個HTML文件(在火狐上是沒問題的)
3 引入的是html格式。引入的文件 只有html內容,不帶head 等信息!
補充:responseText -------------- ajax處理服務器端返回結果
用法:
$("#foot").html($.ajax({url: "../wxpage/foot.html", async: false}).responseText);
console.log($.ajax({
type:"get",
url:"stocks_list.html",
dataType:"html",
async:false,
success:function(data){
$("body").append(data);
funs.init();
}
}).responseText)
333