[英]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.