简体   繁体   English

如何在博客页面中获取搜索栏以搜索选择性文章?

[英]How to get search bar in blog page to search the selective articles?

I am working for my fitness website and I made a blog page where lots of articles are available now, I made a load more button to show more article and script is working fine, now the next thing is I want to put a search bar on top of the page so that user can able to see articles which he write in search bar. 我正在健身网站上工作,并创建了一个博客页面,现在有很多文章可供使用,我单击了“加载更多”按钮以显示更多文章,并且脚本运行正常,现在接下来是我要在其上放置一个搜索栏页面顶部,以便用户可以看到他在搜索栏中写的文章。 I made a script I don't know where is the problem 我做了一个脚本,我不知道问题出在哪里

the code is given below 代码如下

now the html code is: 现在的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>

now the javascript is:- 现在的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";
    }
  }
}

I don't know where is the problem? 我不知道问题出在哪里? please help 请帮忙

Try changing the following line in your for-loop if statement and see if it works: 尝试在for循环中的if语句中更改以下行,看看它是否有效:

h2[i].style.display = "";

to

h2[i].style.display = "block";

It's hard to know exactly what's going on without the full HTML file and logic. 没有完整的HTML文件和逻辑,很难确切知道发生了什么。 Also, not sure if this is a typo, but your HTML is missing an opening for the first sample item. 另外,不确定这是否是错字,但您的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
    ...

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM