优化代码 使按钮突出显示 <buttion 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>
时间: 2023-11-22 18:53:34 浏览: 154
可以将按钮的样式通过CSS进行设置,使其突出显示。同时,可以将JavaScript代码进行简化和优化,如下所示:
HTML代码:
```
<button class="btn" onclick="startNTP()">启动NTP</button>
```
CSS代码:
```
.btn {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
.btn:hover {
background-color: #0069d9;
}
```
JavaScript代码:
```
function startNTP() {
fetch('ntp.php')
.then(response => response.text())
.then(data => alert(data))
.catch(error => console.error(error));
}
```
这样,按钮就会以突出的样式显示,同时JavaScript代码也更加简洁和高效。
相关问题
优化代码 并使该代码能够实现监测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>
下面是优化后的代码,可以实现监测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": "异常"}
]
```
buttion按钮禁用
要禁用一个按钮,可以使用以下代码:
HTML:
```
<button id="myButton">点击我</button>
```
JavaScript:
```
// 获取按钮元素
var myButton = document.getElementById("myButton");
// 禁用按钮
myButton.disabled = true;
```
当按钮被禁用后,用户将无法点击它。如果你想重新启用按钮,可以将 `disabled` 属性设置为 `false`。
阅读全文