[英]How to use ajax to load new page into div
我的網站由兩頁組成。 我正在嘗試將第2頁加載到第1頁的div中,然后顯示第2頁onclick。 我正在嘗試使用以下不起作用的代碼。 我是新手,所以如果這只是一個愚蠢的問題,我很抱歉。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({url:"myPage2.html",success:function(result){
$("#div1").html(result);
}});
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
您在加載網址中有一個#
符號。
另外,你為什么不使用:
$("#div1").load("myPage2.html");
從URL中刪除#
后,您的代碼應該可以正常工作。 這是一個稍微簡潔的版本:
$("button").click(function(){
$("#div1").load("myPage2.html");
});
#myPage2.html中的#符號可能是導致錯誤的原因。
除此之外,如果你願意,你也可以這樣做:
$('button').click(function() {
$('#div1').load('myPage2.html');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.