用javaScript写一个有两个搜索框一个添加一个搜索按钮,搜索出的数据按时间倒序排列,下方是显示数据的表格分页排序
时间: 2023-01-29 21:05:51 浏览: 89
这是一个使用 JavaScript 实现的简单示例:
```
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>JavaScript 表格排序</h2>
<input type="text" id="search1" placeholder="搜索关键字">
<input type="text" id="search2" placeholder="添加关键字">
<button onclick="sortTable()">搜索</button>
<table id="myTable">
<tr>
<th onclick="sortTable(0)">时间</th>
<th onclick="sortTable(1)">关键字</th>
<th onclick="sortTable(2)">描述</th>
</tr>
<tr>
<td>2022-12-20</td>
<td>JavaScript</td>
<td>编程语言</td>
</tr>
<tr>
<td>2022-12-19</td>
<td>HTML</td>
<td>网页标记语言</td>
</tr>
<tr>
<td>2022-12-18</td>
<td>CSS</td>
<td>样式表语言</td>
</tr>
</table>
<script>
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
阅读全文