javascript - 如何在博客页面中获取搜索栏以搜索精选文章?
问题描述
我正在为我的健身网站工作,我创建了一个博客页面,现在有很多文章,我做了一个加载更多按钮来显示更多文章,脚本工作正常,现在接下来我想放一个搜索栏页面顶部,以便用户能够看到他在搜索栏中写的文章。我做了一个脚本我不知道问题出在哪里
代码如下
现在的html代码是:
<input type="text" id="inputarticle" onkeyup="myfunction()" size="162" placeholder="Enter the article name">
<div class="grid-container">
<div class="grid-hidden1">
<img class="blog-cl-1-image">
<h2 > <a href="fat.html">Importance of Fat for human Body</a> </h2>
<h4>August 21 2019</h4>
<p>When you eat fat in your diet the most important thing that matters are the type of fat you eat. New research shows that healthy fats are important for optimal health.</p>
</div>
<div class="grid-hidden1">
<img class="blog-cl-2-image">
<h2> <a href="zinc.html">Role of Zinc in Human Body</a> </h2>
<h4>August 21 2019</h4>
<p>Zinc is an essential nutrient that plays an important role in the human body. As your body doesn’t produce zinc so it is essential to take it from food or supplements.</p>
</div>
</div>
现在的javascript是: -
function myfunction() {
// Declare variables
var input, filter, ul, h2, a,i, txtValue;
input = document.getElementById("inputarticle");
filter = input.value.toUpperCase();
ul = document.getElementsByClassName("grid-container");
h2 = ul.getElementsByTagName("h2");
// Loop through all list items, and hide those who don't match the search query
for (i = 0; i < li.length; i++) {
a = h2[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
h2[i].style.display = "";
} else {
h2[i].style.display = "none";
}
}
}
不知道问题出在哪里?请帮忙
解决方案
尝试更改 for-loop if 语句中的以下行,看看它是否有效:
h2[i].style.display = "";
至
h2[i].style.display = "block";
如果没有完整的 HTML 文件和逻辑,很难确切知道发生了什么。此外,不确定这是否是错字,但您的 HTML 缺少第一个示例项目的开口。
<div class="grid-hidden1">
<img class="blog-cl-1-image">
<h2 > <a href="fat.html">Importance of Fat for human Body</a> </h2>
<h4>August 21 2019</h4>
<p>When you eat fat in your diet the most important thing that matters are the type of fat you eat. New research shows that healthy fats are important for optimal health.</p>
**</div>** <-- MISSING OPENING DIV
...
希望这可以帮助!
推荐阅读
- c++ - 宏参数的扩展如何在c ++中工作
- asp.net-mvc - 使用 Service Worker - 在线时使用获取的数据的 MVC 视图
- php - 为什么在我的 html 文件中找不到 svg 代码行
- r - 为什么 R 找不到函数“getStates”
- matlab - 如何通过 fsolve 函数获得这些具有灵活参数的方程的根?
- java - 我可以强制在 Spring 中包含两个标题中的至少一个吗?
- couchbase - 如何在 Couchbase 中获取今天插入的文档?
- javascript - 展平数组的最有效方法
| T> 在 JavaScript 中 - python - 如何将字典列表更改为定义的数据框格式?
- alfresco - 如何使用 Java 从 ServiceTask 在 Alfresco 中创建新内容?