用ajax加载表格
时间: 2023-07-18 11:18:32 浏览: 52
可以通过以下步骤用 AJAX 加载表格:
1. 创建一个 HTML 页面,其中包含一个按钮和一个表格容器。
2. 在 JavaScript 文件中编写一个函数,使用 AJAX 技术从服务器获取表格数据。
3. 在 AJAX 回调函数中,将表格数据插入表格容器中。
以下是一个示例代码:
HTML 页面:
```
<button onclick="loadTable()">Load Table</button>
<div id="table-container"></div>
```
JavaScript 文件:
```
function loadTable() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var tableData = JSON.parse(xhr.responseText);
var tableHtml = '<table>';
for (var i = 0; i < tableData.length; i++) {
tableHtml += '<tr>';
tableHtml += '<td>' + tableData[i].name + '</td>';
tableHtml += '<td>' + tableData[i].age + '</td>';
tableHtml += '</tr>';
}
tableHtml += '</table>';
document.getElementById('table-container').innerHTML = tableHtml;
}
};
xhr.open('GET', 'table-data.php', true);
xhr.send();
}
```
在上面的代码中,loadTable() 函数使用 AJAX 技术从服务器获取表格数据。AJAX 回调函数将表格数据转换为 HTML 表格,并将其插入表格容器中。请注意,上面的示例代码假设表格数据存储在 table-data.php 文件中,并且以 JSON 格式返回。您需要根据自己的需求更改代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)