DIV CSS 佈局教程網

用 jQuery 的 load() 動態載入其他頁面內容
編輯:JQuery特效代碼     

線上範例:https://demo.jb51.net/js/JQueryload/index.htm

load() 的好處在於只要給它一個網址,它就會把抓回來的內容直接填進指定的元素裡,是分部載入頁面片段最省事的寫法。

完整程式碼

<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>JQuery - Load</title>
<link rel="stylesheet" media="all" type="text/css" href="../CSS/myStyle.css" />
<script type="text/javascript"
  src="https://demo.jb51.net/jslib/jquery/jquery-1.3.2.min.js"></script>
<style type="text/css">
#header { margin-bottom: 1em; padding-bottom: 1em; border-bottom: 1px solid #eee; }
.buttonListArea {
  float: left; width: 150px; padding-right: 10px;
  border-right: 1px solid #eee; margin-right: 10px;
}
.buttonArea { margin: 10px; padding-bottom: 20px; }
#load_content { float: left; width: 550px; text-align: center; }
</style>
<script type="text/javascript">
$(document).ready(function () {
    $('#btnLoad1.button').click(function () {
        $('#load_content').load('loadContent1.htm');
    });
    $('#btnLoad2.button').click(function () {
        $('#load_content').load('loadContent2.htm');
    });
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
  <div id="header">
    <h2>JQuery Load Example</h2>
  </div>
  <div class="buttonListArea">
    <div class="buttonArea">
      <div class="button" id="btnLoad1">Load 1</div>
    </div>
    <div class="buttonArea">
      <div class="button" id="btnLoad2">Load 2</div>
    </div>
  </div>
  <div id="load_content">
    <h2>這是要被載入的區域</h2>
  </div>
</div>
</form>
</body>
</html>

兩個待載入的片段檔內容各自只有一行:

<!-- loadContent1.htm -->
jb51.net

<!-- loadContent2.htm -->
sc.jb51.net

按下不同的按鈕,右側區域的內容就會被替換掉。實務上很常用來做「主頁面不刷新、只換局部內容」的版面,例如切換側邊欄的說明文字或列表。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved