<!--PHP编程实战-->
<!--JSON & Ajax -->
<!--15-9-->
<!--使用XMLHttpRequest抓取HTML-->
<html>
<head>
<style type="text/css">
#generated_content {
border: 1px solid black;
widht: 300px;
background-color: #dddddd;
}
</style>
</head>
<body>
<p><strong>Ajax grabbed plain text containing html:</strong></p>
<div id="generated_content"> </div>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.open("GET", "sample_table.html", true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var message = "";
if (xhr.status = 200) {
message = xhr.responseText;
}
else {
message = "An error has occured making the request";
}
document.getElementById("generated_content").innerHTML = message;
}
}
xhr.send(null);
</script>
</body>
</html>
sample_table.html
<!--表格周围的边框设置为 1 像素宽-->
<table border="1">
<tr><th>foo</th><th>bar</th></tr>
<tr><th>a</th><th>1</th></tr>
<tr><th>b</th><th>2</th></tr>
<tr><th>c</th><th>3</th></tr>
</table>
重点
- xhr.open("GET", "sample_table.html", true);
- message = xhr.responseText;
- sample_table.html也可以补充为一个标准的html文件