
本教程详细介绍了如何在前端卡片搜索功能中,准确地在无匹配结果时显示“未找到卡片”提示。通过重构javascript逻辑,先统一处理所有卡片的显示状态,再根据搜索结果的数量决定是否展示无内容提示,从而解决了原始代码中提示信息显示不准确的问题,提升了用户体验。
在现代Web应用中,动态搜索功能是提升用户体验的关键。当用户在大量数据中查找特定内容时,实时反馈至关重要。其中一个常见需求是:当搜索没有匹配结果时,能够清晰地告知用户“未找到相关内容”。本教程将深入探讨如何使用HTML、CSS和JavaScript实现一个健壮的卡片搜索功能,并确保“未找到卡片”提示在正确时机显示。
原始的JavaScript搜索逻辑通常存在一个常见问题:在遍历卡片时,一旦某张卡片不匹配搜索条件,就立即显示“无内容”提示。这种做法会导致在仍有其他卡片匹配时,或者只有一张卡片不匹配时,不准确地显示“无内容”提示。理想情况下,“无内容”提示应该在所有卡片都遍历完毕,并且没有一张卡片匹配搜索条件时才显示。
原始代码片段示例(存在逻辑问题):
function myFunction() {
// ... 其他变量定义 ...
noContent.style.display = "none"; // 每次搜索前隐藏
for (i = 0; i < cards.length; i++) {
title = cards[i].querySelector(".card-title");
if (title.innerText.toUpperCase().indexOf(filter) > -1) {
cards[i].style.display = "block";
} else {
cards[i].style.display = "none";
noContent.style.display = "flex"; // 问题所在:在循环内就显示了
}
}
}上述代码的症结在于,noContent.style.display = "flex"; 被放在了 else 分支中。这意味着只要有一张卡片不匹配,这个提示就会被设置为显示。如果后续的卡片又匹配了,它会再次被隐藏,但如果最后一张不匹配,它就会保持显示,即使前面有匹配的卡片。这显然不是我们想要的行为。
为了解决上述问题,我们需要将搜索逻辑与“无内容”提示的显示逻辑分离。核心思想是:
首先,确保你的HTML结构包含一个搜索输入框、一个卡片容器和用于显示“无内容”提示的元素。
<div class="input-container">
<input type="text" id="myFilter" class="search-imput" onkeyup="myFunction()" placeholder="Search for names..">
</div>
<div class="card-container" id="myItems">
<!-- 多个 .blog-card 元素 -->
<div class="blog-card">
<div class="blog-card-body">
<h1 class="card-title">Auto Repair</h1>
</div>
<!-- ... 其他卡片内容 ... -->
</div>
<div class="blog-card">
<div class="blog-card-body">
<h1 class="card-title">Dentist</h1>
</div>
<!-- ... 其他卡片内容 ... -->
</div>
<!-- 更多卡片... -->
</div>
<div class="no-content" id="no-content" style="display: none;">
<h1>there are no more blog post with this term</h1>
</div>注意事项:
以下是经过优化的JavaScript函数,它实现了上述分离逻辑:
function myFunction() {
// 1. 获取DOM元素
const input = document.getElementById("myFilter");
const noContent = document.getElementById("no-content");
const filter = input.value.toUpperCase(); // 获取并转换为大写以便进行不区分大小写的搜索
const cardContainer = document.getElementById("myItems");
// 将HTMLCollection转换为数组,以便使用forEach和filter等数组方法
const cards = Array.from(cardContainer.getElementsByClassName("blog-card"));
// 2. 每次搜索前,首先隐藏所有卡片
cards.forEach(card => card.style.display = "none");
// 3. 过滤匹配搜索条件的卡片
const matchingCards = cards.filter(
card => card.querySelector(".card-title").innerText.toUpperCase().includes(filter)
);
// 4. 显示所有匹配的卡片
matchingCards.forEach(card => card.style.display = "block");
// 5. 根据匹配卡片的数量,决定是否显示“无内容”提示
noContent.style.display = (matchingCards.length === 0 ? "flex" : "none");
}代码解析:
为了美观和功能,这里提供了一套完整的CSS样式。特别是 .no-content 类的样式,确保它在显示时能够居中并提供良好的视觉反馈。
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@500&family=Open+Sans:wght@700&display=swap');
/* 输入框样式 */
.input-container {
display: flex;
justify-content: center;
width: 100%;
}
.input-container .search-imput {
margin: 20px;
width: 50%;
height: 40px;
border-radius: 30px;
border: 2px solid #6F77E9;
}
::placeholder {
color: #161663;
font-weight: bold;
text-align: center;
font-size: 1em;
}
/* 卡片容器样式 */
.card-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
flex-direction: row;
margin: 50px;
width: 100%; /* 注意这里将 100%px 改为 100% */
}
/* 单个卡片样式 */
.blog-card {
width: 490px;
height: 470px;
margin: 15px;
border-radius: 20px;
-webkit-box-shadow: 0px 0px 4px 0px rgba(22, 22, 99, 0.30);
box-shadow: 0px 0px 4px 0px rgba(22, 22, 99, 0.30);
}
.blog-card p {
font-family: 'Montserrat', sans-serif;
margin: 0px;
padding: 0px 30px;
}
.blog-card .blog-card-head img {
padding: 20px;
width: 92%;
border-radius: 30px;
}
.blog-card .blog-card-head h2 {
font-family: 'Open Sans', sans-serif;
text-align: center;
color: #161663;
}
.blog-card .blog-card-footer,
.blog-card .blog-card-body {
display: flex;
align-items: center;
}
.blog-card .blog-card-body {
padding: 0px 20px;
}
.blog-card .blog-card-body img {
width: 50px;
}
.blog-card .blog-card-body h1 {
font-family: 'Open Sans', sans-serif;
font-size: 20px;
color: #161663;
}
.blog-card .blog-card-footer {
padding: 0px 20px;
}
.blog-card .blog-card-footer a {
display: flex;
justify-content: center;
align-items: center;
font-family: 'Montserrat', sans-serif;
text-decoration: none;
}
.blog-card .blog-card-footer img {
margin: 10px;
width: 30px;
}
/* 无内容提示样式 */
.no-content {
width: 100vw; /* 占据整个视口宽度 */
height: 50vh; /* 占据视口高度的一半 */
display: flex; /* 使用Flexbox进行布局 */
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中 */
}
.no-content h1 {
font-size: 20px;
padding: 0px 20px;
font-family: 'Montserrat', sans-serif;
color: #161663;
}通过上述优化,我们实现了一个更加准确和用户友好的卡片搜索功能。关键的改进在于:
这种模式不仅适用于卡片搜索,也适用于任何需要根据动态数据结果来显示或隐藏特定UI元素的场景,是前端开发中一种常见的、推荐的实践方法。
以上就是实现动态卡片搜索无结果提示的优化方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号