美文网首页
PHP编程实战15-9

PHP编程实战15-9

作者: 海边拾贝 | 来源:发表于2015-11-09 20:04 被阅读0次
<!--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文件

相关文章

网友评论

      本文标题:PHP编程实战15-9

      本文链接:https://www.haomeiwen.com/subject/qhchhttx.html