
本文旨在解决动态卡片搜索中“未找到卡片”提示显示不准确的问题。通过优化javascript逻辑,我们展示了一种更健壮的方法:首先隐藏所有卡片,然后根据搜索条件过滤并仅显示匹配的卡片,最后根据匹配结果的数量精确控制“无内容”提示的可见性,确保用户体验的准确性和流畅性。
在网页开发中,实现带有搜索功能的卡片布局是常见的需求。用户输入搜索关键词后,页面应动态显示匹配的卡片,并在没有匹配结果时提供友好的提示。然而,不恰当的逻辑可能导致“无结果”提示过早或错误地出现。本教程将详细探讨如何使用HTML、CSS和JavaScript构建一个健壮的卡片搜索功能,并特别关注“无结果”提示的正确显示。
原始的JavaScript代码在处理搜索逻辑时存在一个常见误区。它在遍历卡片时,一旦遇到不匹配的卡片就立即将“无内容”提示设置为可见。这意味着,即使后续还有匹配的卡片,或者在搜索过程中仍有卡片可见,这个提示也可能被错误地显示出来。
function myFunction() {
  var input, filter, cards, cardContainer, noContent, title, i, cardExist;
  input = document.getElementById("myFilter");
  noContent = document.getElementById("no-content");
  filter = input.value.toUpperCase();
  cardContainer = document.getElementById("myItems");
  cards = cardContainer.getElementsByClassName("blog-card");
  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 可能导致误判
      noContent.style.display = "flex"; 
    }
  }
}上述代码的问题在于 noContent.style.display = "flex"; 语句被放置在 else 分支内。这意味着只要有一张卡片不匹配,"no-content" 元素就会被显示。如果搜索结果中包含多张卡片,但其中只有一张不匹配,"no-content" 元素也会被错误地显示出来,从而误导用户。
为了确保“无结果”提示仅在确实没有任何卡片匹配搜索条件时才显示,我们需要调整逻辑,采用“先处理所有卡片,再统一判断结果”的策略。
首先,确保你的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>注意: no-content 元素在初始状态下应设置为 display: none; 或使用 hidden 属性,以确保它在页面加载时不会显示。
为了美观和功能性,需要为卡片、搜索框和“无内容”提示定义相应的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%px;  此处应为 width: 100%; 或删除 px */
}
.blog-card {
  width: 490px;
  height: 470px;
  margin: 15px;
  border-radius: 20px;
  box-shadow: 0px 0px 4px 0px rgba(22, 22, 99, 0.30);
}
/* ... 其他卡片内部元素样式 ... */
/* 无内容提示样式 */
.no-content {
  width: 100vw;
  height: 50vh;
  display: flex; /* 默认设置为 flex,通过 JS 控制显示/隐藏 */
  justify-content: center;
  align-items: center;
}
.no-content h1 {
  font-size: 20px;
  padding: 0px 20px;
  font-family: 'Montserrat', sans-serif;
  color: #161663;
}核心优化在于JavaScript函数 myFunction()。我们将采用以下步骤:
function myFunction() {
  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"));
  // 步骤 1: 首先隐藏所有卡片,重置显示状态
  cards.forEach(card => card.style.display = "none");
  // 步骤 2: 过滤出所有匹配搜索条件的卡片
  const matchingCards = cards.filter(
    card => card.querySelector(".card-title").innerText.toUpperCase().includes(filter)
  );
  // 步骤 3: 显示所有匹配的卡片
  matchingCards.forEach(card => card.style.display = "block");
  // 步骤 4: 根据匹配卡片的数量,决定是否显示“无内容”提示
  noContent.style.display = (matchingCards.length === 0 ? "flex" : "none"); 
}通过上述优化,我们解决了动态卡片搜索中“无结果”提示显示不准确的问题。这种“先隐藏全部,再显示匹配,最后判断总数”的策略是处理此类交互功能的标准做法,它不仅提升了用户体验的准确性,也使JavaScript代码更加清晰和易于维护。在构建类似的动态内容过滤功能时,请务必采纳这种分离逻辑和统一判断的思路。
以上就是如何实现卡片搜索无结果时准确显示“未找到卡片”提示的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号