若要把某個 HTML 檔載入到頁面的指定區域,可以使用 jQuery 的 load()。
先載入 jQuery:
<script src="js/jquery.js"></script>
接著在 ready 事件中指定目標 div:
$(function(){ $("div#top_sidebar").load("top_sidebar.html"); });
這段程式會在頁面 DOM 準備好後,把 top_sidebar.html 的內容載入 id 為 top_sidebar 的 div。若需要每次更新資料,也可搭配 timestamp 參數避免快取。