href 値を取得し、適切なセレクターとajax()やload( )などの適切な jQuery ajax 関数を使用して、リンクされたページのコンテンツをロードできます。
$(document).ready(function() {
$('#list > li a').click(function(event) {
var page = this.attr("href");
// Here should go your ajax code or what-have-you to load the content.
// If you use an ajax function, the url will be equal to the var page.
event.preventDefault();
$.ajax({
type: "POST", // or "GET" may be better for your needs
url: page,
data: "",
success: function(content) {
console.log("ajax success");
$("#divID").html(content);
},
error: function() {
alert("ajax error");
},
complete: function() {
console.log("ajax complete");
}
});
});
});
<ul id="list">
<li><a href="mypage.php">Mypage</a> </li>
<li><a href="mypage2.php">Mypage2</a> </li>
</ul>
コードには、jQuery に ID 'list' を見つけるように指示するハッシュがありませんでした。「#list > li a」は、コード ブロックを #list のすべての「li a」子に適用するよう jQuery に指示します。