简体   繁体   English

我正在尝试使用 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

<form action="" class="search-form">
    <input type="search" id="search-box" placeholder="search here...">
    <label for="search-box" class="fa fa-search" aria-hidden="true"></label>
</form>

My javascript code:我的 javascript 代码:

let searchForm  = document.querySelector('.search-form');

document.querySelector('#search-b t n').on click = () =>{
    searchForm.classList.toggle('active');
}

the error says:错误说:

Uncaught TypeError: Cannot set properties of null (setting 'onclick')
    at script.js:5

Firstly, your code seems strange ans lot of space appear in it.首先,您的代码看起来很奇怪,因为其中出现了很多空间。 I edit to remove them, but check that all works are complete.我编辑删除它们,但检查所有工作是否已完成。

Then, the error comes because it failed to find the objet with id search-b tn .然后,错误来了,因为它未能找到 id search-b tn的对象。 Even without space, it doesn't works because in your form, there is no div with this id.即使没有空格,它也不起作用,因为在您的表单中,没有具有此 ID 的 div。

You should use something like this:你应该使用这样的东西:

document.querySelector('#search-box`)

Because this ID exist.因为这个ID存在。

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

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