Bỏ qua đến nội dung
Bỏ qua điều hướng
Đăng nhập
MT.NET.VN

Viewbh Json Tut 11 Html Json 0 0 2023 04 04 08 07 33

Tìm kiếm
Thuộc khóa học · baihoc_sub = 23

JSon cơ bản

Bài 1 / 1 trong khóa học.

Xem khóa học
100%
Bài trướcĐây là bài đầu tiên Bài sauĐây là bài cuối cùng
MT LEARNING · LESSON

JSON tut 11: HTML JSON 0 (0)

JSON tut 11: HTML JSON 0 (0)

JSON tut 11: HTML JSON 0 (0)

JSON có thể dễ dàng được dịch sang JavaScript.

JavaScript có thể được sử dụng để tạo HTML trong các trang web của bạn.


Bảng HTML

Tạo bảng HTML với dữ liệu nhận được dưới dạng JSON:

Thí dụ

const dbParam = JSON.stringify({table:"customers",limit:20});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  myObj = JSON.parse(this.responseText);
  let text = "<table border='1'>"
  for (let x in myObj) {
    text += "<tr><td>" + myObj[x].name + "</td></tr>";
  }
  text += "</table>"
  document.getElementById("demo").innerHTML = text;
}
xmlhttp.open("POST", "json_demo_html_table.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

Hãy tự mình thử »


Bảng HTML động

Tạo bảng HTML dựa trên giá trị của menu thả xuống:  Chọn một sự lựa chọn: Khách hàng Các sản phẩm Các nhà cung cấp 

Thí dụ

<select id="myselect" onchange="change_myselect(this.value)">
  <option value="">Choose an option:</option>
  <option value="customers">Customers</option>
  <option value="products">Products</option>
  <option value="suppliers">Suppliers</option>
</select><script>
function change_myselect(sel) {
  const dbParam = JSON.stringify({table:sel,limit:20});
  const xmlhttp = new XMLHttpRequest();
  xmlhttp.onload = function() {
    const myObj = JSON.parse(this.responseText);
    let text = "<table border='1'>"
    for (let x in myObj) {
      text += "<tr><td>" + myObj[x].name + "</td></tr>";
    }
    text += "</table>"
    document.getElementById("demo").innerHTML = text;
  }
  xmlhttp.open("POST", "json_demo_html_table.php");
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.send("x=" + dbParam);
}
</script>

Hãy tự mình thử »



Danh sách thả xuống HTML

Tạo danh sách thả xuống HTML với dữ liệu nhận được dưới dạng JSON:

Thí dụ

const dbParam = JSON.stringify({table:"customers",limit:20});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);
  let text = "<select>"
  for (let x in myObj) {
    text += "<option>" + myObj[x].name;
  }
  text += "</select>"
  document.getElementById("demo").innerHTML = text;
  }
}
xmlhttp.open("POST", "json_demo_html_table.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

Hãy tự mình thử »

ĐÁNH GIÁ NỘI DUNG 0.0/5 0 lượt đánh giá
Bạn thấy nội dung này thế nào?
Mỗi trình duyệt/tài khoản được chấm lại sau 24 giờ.
Báo bài lỗiGiúp MT.Net.VN phát hiện nội dung cần kiểm tra
Báo lỗi được ghi vào hệ thống để Admin kiểm tra. Không dùng chức năng này để gửi hỗ trợ cá nhân.
Home Dịch vụ Khóa học CPanel