简体   繁体   English

我正在尝试在 VISUAL STUDIO 中使用 javascript 创建一个搜索栏,但我还没有成功

[英]I am trying to create a search bar using javascript in VISUAL STUDIO but I have not yet succeeded

html File: I have used some divs and inside i have introduced some titles and images. html 文件:我使用了一些 div,在里面我介绍了一些标题和图像。

In the search bar i just want that when i enter a title the full div appears on the screen when i search.在搜索栏中,我只希望当我输入标题时,当我搜索时,完整的 div 会出现在屏幕上。

<body>
    <div class="input">
        <input type="text" placeholder="Search.." id="searchbar" onkeyup="search_bar()">
        <button type="submit"><i class="fa fa-search"></i></button>
    </div>
    <div class="s10plus" id="divs">
        <h2>Samsung Galaxy S10 Plus</h2>
        <img src="Images/s10plus.png"></img>
        <p id="precios10">Price : $600</p>
    </div>
    <div class="samsunga10">
        <h2>Samsung Galaxy A10</h2>
        <img src="Images/a10.png"></img>
        <p id="precioa">Price:$600</p>
    </div>
    <div class="samsunga20">
        <h2>Samsung Galaxy A20</h2>
        <img src="Images/a20.png"></img>
        <p id="precio">Price:$600</p>
    </div>
</body>
  

file.js文件.js

function search_bar() {
    var a;
    var search = document.getElementById('searchbar');
    var filter = search.input.toLowerCase();
    var ul = document.getElementsByClassName('s10plus');
    var nodes = ul.getElementsByTagName('h2').length;

    for (i = 0; i < nodes; i++) {
        a = nodes[i].getElementsByTagName('h2')[0];
        if (a.innerHTML.toLowerCase().indexOf(filter) > -1) {
            nodes[i].style.display = "";
        } else {
            nodes[i].style.display = "none";
        }
    }
}

Could you share a Codepen or Codesandbox or add a snipped here in SO?你能分享一个 Codepen 或 Codesandbox 或者在 SO 中添加一个片段吗?

in your Js code: "li" is not defined which makes it hard to understand your code exactly.在您的 Js 代码中:“li”未定义,这使得您很难准确理解您的代码。 Also, setting display: "", is not a valid css property.此外,设置显示:“”不是有效的 css 属性。 I recommend setting it to display: "inherit".我建议将其设置为显示:“继承”。

The class "s10plus" at: class“s10plus”位于:

          var ul = document.getElementsByClassName('s10plus');

will always get 1 element since in your html only one div has that class.将始终获得 1 个元素,因为在您的 html 中只有一个 div 具有该 class。

Try to adjust these issues and see if you can get any further尝试调整这些问题,看看是否可以进一步

暂无
暂无

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

相关问题 我正在尝试创建一个圆形进度条,但我无法访问 javascript 变量,为什么? - I am trying to create a circle progress bar, but I can not access the javascript variables, why? Vuejs / javascript / 我正在尝试连接一个搜索栏来搜索我的表格并根据我的输入显示结果 - Vuejs / javascript / I am trying to connect a search bar that searches my table and displays the results according to my input 我正在尝试使用 html、CSS 和 java 脚本创建一个弹出式搜索栏。 但我最终在控制台上遇到错误 - I am trying to create a pop up search bar using html, CSS and java script. but I end up getting an error on my console 我正在尝试使用JavaScript创建一个计算器。 - I am trying to create a calculator with javascript. 我正在尝试在 Visual Studio Code 中使用 JavaScript 编写 discord bot,但我不断收到命令名称必须为小写的错误 - I am trying to code a discord bot with JavaScript in Visual Studio Code, but I keep getting an error that Command name must be lowercase 我在 javascript 文件中的 Visual Studio 代码中遇到错误 - i am getting an error in visual studio code in javascript files 我正在尝试用 JavaScript 编写一个 Discord 机器人,但遇到了问题 - I am trying to write a Discord bot in JavaScript and have encountered a problem 搜索栏似乎表现得好像它的 position 是粘性的,但我将它设置为固定但当我滚动时它仍然向下移动 - Search bar seems to be acting as if its position is sticky but I have it set to fixed yet it still moves down when I scroll 我正在尝试使用用户通过JavaScript警报框输入的信息在HTML页面上创建链接 - I am trying to create a link on an HTML page using information that a user inputs via JavaScript alert box 我正在尝试使用 javascript 从 JSON 文件创建动态 html 选项 - I am trying to create dynamic html options from a JSON file using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM