javascript - jQuery突出显示搜索词的文本
问题描述
我想突出显示(或背景颜色)过滤行中搜索词的输入文本 $('#Search').val()。我试图用跨度包装它,添加一个类并直接添加 CSS - 不工作......
搜索框(输入)的 id="Search"
<p>
<span>Search:</span>
<input type="text" id="Search" name="Search" maxlength="20" placeholder="Search" />
</p>
表 id="vw"
<table id='<?php echo $vw;'>
<thead>
<tr>
<th>
<th>
<th>
</tr>
</thead>
<tbody>
<tr>
<td>
<td>
<td>
</tr>
</tbody>
</table>
$('#Search').keyup(function() {
// only search when there are 3 or more characters in the textbox
if ($('#Search').val().length > 2) {
// hide all rows
$('#' + vw + ' tr').hide();
// show the header row
$('#' + vw + ' tr:first').show();
// show the matching rows (using the containsNoCase)
$('#' + vw + ' tr td:containsNoCase(\'' + $('#Search').val() + '\')').parent().show();
// HERE - Want to hightlight(or background-color) the input text $('#Search').val() in the filtered row
// change css class to show all white records
$('#' + vw + ' tr:nth-of-type(odd)').css('background', '#fff');
} else if ($('#Search').val().length == 0) {
// if the user removed all of the text, reset the search
resetSearch();
}
解决方案
推荐阅读
- python - 如何使用库文件作为独立文件创建 setup.py
- activemq-artemis - 在 Artemis Webconsole 中设置标题的问题
- python - Discord.py 重写 - 如何以列表的形式获取用户的权限?
- python - 从 .py 文件编译 .exe 时,如何修复“ImportError: DLL load failed”?
- date - 在张量流或张量流扩展中转换日期
- laravel - 如何使用 Eloquent 在两个表之外的不同数据库中查找相关值?
- selenium - 每次测试用例运行时从 Excel 文件中读取不同的值
- sql - 使用两个 WHERE 子句的 SQL 查询帮助
- html - 在多个按钮之间切换文本
- full-text-search - 在 Sublime Text 中,是否有一种方法可以仅在版本控制下搜索文件?