我实际上写了一个不同问题的答案,这似乎适用于这里:https ://stackoverflow.com/a/10012302/166661
你有一个服务器会返回给你信息——你可以把这个信息放在一个IFRAME
... 或者你可以调用一个 JavaScript 函数来检索那个信息并将它显示在DIV
你在页面上留出的位置 ( ) 中。
这是一个示例 HTML 页面,它将使用 AJAX 从服务器检索信息
<html>
<head>
<script type="text/javascript">
function getAreaInfo(id)
{
var infoBox = document.getElementById("infoBox");
if (infoBox == null) return true;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) alert(xhr.status);
infoBox.innerHTML = xhr.responseText;
};
xhr.open("GET", "info.php?id=" + id, true);
xhr.send(null);
return false;
}
</script>
<style type="text/css">
#infoBox {
border:1px solid #777;
height: 400px;
width: 400px;
}
</style>
</head>
<body onload="">
<p>AJAX Test</p>
<p>Click a link...
<a href="info.php?id=1" onclick="return getAreaInfo(1);">Area One</a>
<a href="info.php?id=2" onclick="return getAreaInfo(2);">Area Two</a>
<a href="info.php?id=3" onclick="return getAreaInfo(3);">Area Three</a>
</p>
<p>Here is where the information will go.</p>
<div id="infoBox"> </div>
</body>
</html>
这里是将信息返回给 HTML 页面的 info.php:
<?php
$id = $_GET["id"];
echo "You asked for information about area #{$id}. A real application would look something up in a database and format that information using XML or JSON.";
?>
希望这可以帮助!