javascript - 在搜索表中为“主页”和“主页”提供相同的结果
问题描述
这是一个搜索表,并且运行良好。但我希望这样写“主页”和这样的“主页”。在这两种情况下,它都会显示主页的结果。“关于我们”和“联系我们”的相似之处。
代码如下。请问谁能帮帮我
#myInput {
padding: 12px 20px 12px 40px;
border: 5px solid #ddd;
margin-bottom: 12px;
border-radius:20px;
}
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Search your product here">
<table id="myTable" align="center" border="5" width>
<tr class="header">
<th>Header 1 </th>
<th>Header 2</th>
</tr>
<tr>
<td><center>
<br><b>Home page</b></a></center></td>
<td> </td>
</tr>
<tr>
<td><center>
<br><b>About us</b></a></center></td>
<td>
</td>
</tr>
<tr>
<td><center>
<br><b>Contact us</b></a></center></td>
<td>
</td>
</tr>
</table>
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
解决方案
使用正则表达式替换空格
filter = filter.replace(/\s/g, '')
编辑评论
filter = filter.replace(/\s/g,'')
txtValue = txtValue.replace(/\s/g, '')
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
推荐阅读
- c# - 使用 OPENJSON 将两个数组的对象插入 SQL 数据库
- excel - 通过 excel vba 实现的 IE 自动化失去了对新 URL 的关注
- java - 获取 JList 组件的问题
- java - 从客户端(Android-Mobile)向服务器(Python)发送消息
- c - 打印文件的特定行
- jquery - 创建可在 Spotfire 仪表板的所有选项卡中使用的浮动文档属性控件
- tensorflow - 如何导入特定版本的 Tensorflow.js?
- php - 使用 verot class.upload.php 在数组中上传
- pyspark - 基于列存储索引创建新数据框
- javascript - 将 ViewBag 更改为 Json 数组以在 .net MVC 中绘制条形图