JSON HTML

JSON 非常易于转译为 JavaScript。

JavaScript 可用于在网页中生成 HTML。


HTML 表格

使用作为 JSON 接收到的数据来生成表格:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h2>做一个基于 JSON 数据的表格。</h2>
  5. <p id="demo">
  6. <script>
  7. var obj, dbParam, xmlhttp, myObj, x, txt = "";
  8. obj = { table: "customers", limit: 20 };
  9. dbParam = JSON.stringify(obj);
  10. xmlhttp = new XMLHttpRequest();
  11. xmlhttp.onreadystatechange = function() {
  12. if (this.readyState == 4 && this.status == 200) {
  13. myObj = JSON.parse(this.responseText);
  14. txt += "<table border='1'>"
  15. for (x in myObj) {
  16. txt += "<tr><td>" + myObj[x].name + "</td></tr>";
  17. }
  18. txt += "</table>"
  19. document.getElementById("demo").innerHTML = txt;
  20. }
  21. };
  22. xmlhttp.open("POST", "json_demo_db_post.php", true);
  23. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  24. xmlhttp.send("x=" + dbParam);
  25. </script>
  26. </body>
  27. </html>

动态 HTML 表格

使 HTML 表格基于下拉列表的值:选择一个选项

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h2>做一个基于下拉列表值的表格。</h2>
  5. <select id="myselect" onchange="change_myselect(this.value)">
  6. <option value="">选择一个选项:
  7. <option value="customers">客户
  8. <option value="products">商品
  9. <option value="suppliers">供应商
  10. </select>
  11. <p id="demo">
  12. <script>
  13. function change_myselect(sel) {
  14. var obj, dbParam, xmlhttp, myObj, x, txt = "";
  15. obj = { table: sel, limit: 20 };
  16. dbParam = JSON.stringify(obj);
  17. xmlhttp = new XMLHttpRequest();
  18. xmlhttp.onreadystatechange = function() {
  19. if (this.readyState == 4 && this.status == 200) {
  20. myObj = JSON.parse(this.responseText);
  21. txt += "<table border='1'>"
  22. for (x in myObj) {
  23. txt += "<tr><td>" + myObj[x].name + "</td></tr>";
  24. }
  25. txt += "</table>"
  26. document.getElementById("demo").innerHTML = txt;
  27. }
  28. };
  29. xmlhttp.open("POST", "json_demo_db_post.php", true);
  30. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  31. xmlhttp.send("x=" + dbParam);
  32. }
  33. </script>
  34. </body>
  35. </html>

HTML 下拉列表

用接收到的 JSON 数据来生成 HTML 下拉列表:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h2>做一个基于 JSON 数据的下拉列表。</h2>
  5. <p id="demo">
  6. <script>
  7. var obj, dbParam, xmlhttp, myObj, x, txt = "";
  8. obj = { table: "customers", limit: 20 };
  9. dbParam = JSON.stringify(obj);
  10. xmlhttp = new XMLHttpRequest();
  11. xmlhttp.onreadystatechange = function() {
  12. if (this.readyState == 4 && this.status == 200) {
  13. myObj = JSON.parse(this.responseText);
  14. txt += "<select>"
  15. for (x in myObj) {
  16. txt += "<option>" + myObj[x].name;
  17. }
  18. txt += "</select>"
  19. document.getElementById("demo").innerHTML = txt;
  20. }
  21. };
  22. xmlhttp.open("POST", "json_demo_db_post.php", true);
  23. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  24. xmlhttp.send("x=" + dbParam);
  25. </script>
  26. </body>
  27. </html>