优化代码 并使该代码能够实现监测ntp客户端连接服务器的状态 且将其绘制成表格 <html> <head> <meta charset="utf-8"> <title>NTP</title> <script src="jquery-3.4.1.min.js"></script> <style type> .btn { background-color: #4CAF50; border: none; color: white; padding: 10px 15px; text-align: center; text-decoration: none; display: inline-block; font-size: 10px; margin: 4px 2px; transition-duration: 0.4s; cursor: pointer; } .btn:hover { background-color: white; color: #4CAF50; border: 2px solid #4CAF50; } </style> </head> <body> <buttion class = "btn" onclick="startNTP()">启动NTP</buttion> <script> function startNTP(){ var xhr = new XMLHttpRequest(); xhr.open('GET','ntp.php',true); xhr.onload = function(){ alert(xhr.responseText); }; xhr.send(); } </script> </body> </html>
时间: 2023-11-27 14:50:45 浏览: 92
下面是优化后的代码,可以实现监测ntp客户端连接服务器的状态,并将其绘制成表格:
<html>
<head>
<meta charset="utf-8">
<title>NTP</title>
<script src="jquery-3.4.1.min.js"></script>
<style type="text/css">
table {
font-family: Arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #ddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.btn {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 10px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}
.btn:hover {
background-color: white;
color: #4CAF50;
border: 2px solid #4CAF50;
}
</style>
</head>
<body>
<button class="btn" onclick="startNTP()">启动NTP</button>
<table id="ntpTable">
<thead>
<tr>
<th>服务器地址</th>
<th>状态</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
function startNTP(){
var xhr = new XMLHttpRequest();
xhr.open('GET','ntp.php',true);
xhr.onload = function(){
updateTable(xhr.responseText);
};
xhr.send();
}
function updateTable(response) {
var ntpData = JSON.parse(response);
var tableBody = document.querySelector('#ntpTable tbody');
tableBody.innerHTML = '';
ntpData.forEach(function(ntp) {
var row = document.createElement('tr');
var serverCell = document.createElement('td');
serverCell.textContent = ntp.server;
row.appendChild(serverCell);
var statusCell = document.createElement('td');
statusCell.textContent = ntp.status;
row.appendChild(statusCell);
tableBody.appendChild(row);
});
}
</script>
</body>
</html>
需要确保 `ntp.php` 返回的是一个JSON格式的数据,例如:
```
[
{"server": "ntp1.example.com", "status": "正常"},
{"server": "ntp2.example.com", "status": "异常"}
]
```
阅读全文