
本教程旨在指导开发者如何使用 jQuery 实现一个交互式表格,当用户在输入框中输入内容时,表格中匹配的单元格以及紧随其后的单元格会被高亮显示。我们将详细讲解代码实现,并提供可直接运行的示例,帮助你快速掌握该技巧。
首先,我们需要一个包含输入框和表格的 HTML 结构。输入框用于接收用户的输入,表格则用于显示数据并进行高亮。
<div class="container">
<div class="row">
<div class="col-md-6">
<label for="">Basic</label>
<input class="form-control" type="text" name="cb" id="cb" autocomplete="off" />
</div>
</div>
</div>
<table class="table table-responsive">
<tr>
<td>
<h6>Current Level</h6>
</td>
</tr>
<tr>
<td>
<table id="le10" class="table table-responsive table-striped">
<tr>
<td>56100</td>
</tr>
<tr>
<td>57800</td>
</tr>
<tr>
<td>59500</td>
</tr>
<tr>
<td>61300</td>
</tr>
<tr>
<td>63100</td>
</tr>
<tr>
<td>65000</td>
</tr>
<tr>
<td>67000</td>
</tr>
<tr>
<td>69000</td>
</tr>
<tr>
<td>71100</td>
</tr>
<tr>
<td>73200</td>
</tr>
<tr>
<td>75400</td>
</tr>
<tr>
<td>77700</td>
</tr>
</table>
</td>
</tr>
</table>这里使用了 Bootstrap 的 CSS 类来增强页面美观性和响应式。
接下来,定义用于高亮单元格的 CSS 类。
立即学习“前端免费学习笔记(深入)”;
.highlight {
color: red;
background-color: yellow;
font-weight: bold;
}
.highlight2 {
color: blue;
background-color: greenyellow;
font-weight: bold;
}highlight 类用于高亮匹配的单元格,highlight2 类用于高亮紧随其后的单元格。可以根据实际需求调整颜色和样式。
这是实现核心功能的 jQuery 代码:
$(function () {
$('#cb').on('change keyup', function() {
var search = $(this).val();
$('table tr td').filter(function() {
if($(this).text() == search){
$(this).parent('tr').addClass('highlight');
$(this).parent('tr').closest('tr').next().addClass('highlight2');
}else{
$(this).parent('tr').removeClass('highlight');
$(this).parent('tr').closest('tr').next().removeClass('highlight2');
}
})
});
});这段代码的功能如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Table Highlight</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.highlight {
color: red;
background-color: yellow;
font-weight: bold;
}
.highlight2 {
color: blue;
background-color: greenyellow;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<label for="">Basic</label>
<input class="form-control" type="text" name="cb" id="cb" autocomplete="off" />
</div>
</div>
</div>
<table class="table table-responsive">
<tr>
<td>
<h6>Current Level</h6>
</td>
</tr>
<tr>
<td>
<table id="le10" class="table table-responsive table-striped">
<tr>
<td>56100</td>
</tr>
<tr>
<td>57800</td>
</tr>
<tr>
<td>59500</td>
</tr>
<tr>
<td>61300</td>
</tr>
<tr>
<td>63100</td>
</tr>
<tr>
<td>65000</td>
</tr>
<tr>
<td>67000</td>
</tr>
<tr>
<td>69000</td>
</tr>
<tr>
<td>71100</td>
</tr>
<tr>
<td>73200</td>
</tr>
<tr>
<td>75400</td>
</tr>
<tr>
<td>77700</td>
</tr>
</table>
</td>
</tr>
</table>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function () {
$('#cb').on('change keyup', function() {
var search = $(this).val();
$('table tr td').filter(function() {
if($(this).text() == search){
$(this).parent('tr').addClass('highlight');
$(this).parent('tr').closest('tr').next().addClass('highlight2');
}else{
$(this).parent('tr').removeClass('highlight');
$(this).parent('tr').closest('tr').next().removeClass('highlight2');
}
})
});
});
</script>
</body>
</html>通过本教程,你应该能够掌握如何使用 jQuery 高亮 HTML 表格中匹配的单元格及其相邻单元格。这个技巧可以应用于各种交互式表格应用中,提升用户体验。
以上就是使用 jQuery 高亮 HTML 表格单元格及其相邻单元格的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号