开发者问题收集

未捕获的类型错误:无法读取 (索引) 处的 null 属性 (读取“addEventListener”):72

2021-12-18
7442
let searchBtn = document.querySelector("#Search-btn");
let searchBar = document.querySelector(".search-bar-container");

searchBtn.addEventListener("click", () => {
  searchBtn.classlist.toggle("fa-times");
  searchBar.classList.toggle("active");
});
let searchBtn = document.querySelector ( '#Search-btn');
let searchBar = document.querySelector ( '.search-bar-container');

searchBtn.addEventListener('click', () =>{

    searchBtn.classlist.toggle('fa-times');
    searchBar.classList.toggle('active');
});
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;300;400;600;700&display=swap');

:root {
    --orange:#ffa500;
    
}

*{
    font-family: 'Nunito', sans-serif;
    margin: 0; padding: 0;
    box-sizing: border-box;
    text-transform: capitalize;
    outline: none; border: none;
    transition: all .2s linear;
    text-decoration: none;
    
}

*::selection{
    background: var(--orange);
    color: #fff;
}

html{
    font-size: 62.5%;
    overflow-x: hidden;
    scroll-padding-top: 6rem;
    scroll-behavior: smooth;
}

header{
    position: fixed;
    top: 0; left: 0; right: 0;
    background: #333;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2rem 9%;
}

header .logo {
    font-size: 2rem;
    font-weight: bolder;
    color: #fff;
    text-transform: uppercase;
}

header .logo span {
    color: var(--orange);
}

header .navbar a{
    font-size: 2rem;
    color: #fff;
    margin:0 .8rem;
}

header .navbar a:hover{
    color: var(--orange);
}

header .icons i{
    font-size: 2.5rem;
    color:#fff;
    cursor: pointer;
    margin-right: 2rem;
}

header .icons i:hover{
    color: var(--orange);
}

header .search-bar-container{
    position: absolute;
    top: 100%; left: 0; right: 0;
    padding: 1.5rem 2rem;
    background: #333;
    border-top: .1rem solid rgba(225, 225, 225, .2);
    display: flex;
    align-items: center;
    z-index: 1001;
    clip-path: polygon(0 0, 100% 0, 0 0);
}

header .search-bar-container.active{
    clip-path: polygon(0 0, 100% 0, 0 0);
}

#search-bar {

    width: 100%;
    padding: 1rem;
    text-transform: none;
    color: #333;
    font-size: 1.7rem;
    border-radius: 100rem;
}

header .search-bar-container label{
    color: #fff;
    cursor: pointer;
    font-size: 3rem;
    margin-left: 1.5rem;
}

header .search-bar-container label:hover {
    color: var(--orange);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Travel Agency</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    
<header>
    <a href="#" class="logo"> <span>T</span>ravel</a>

    <nav class="navbar">
        <a href="#home">home</a>
        <a href="#book">book</a>
        <a href="#package">package</a>
        <a href="#services">services</a>
        <a href="#review">review</a>
        <a href="#contact">contact</a>
    </nav>

    <div class="icons">
        <i class="fa fa-search" id="search-btn"></i>
        <i class="fa fa-user" id="login-btn"></i>
    </div>

    <form action="" class="search-bar-container">
        <input type="search" id="search-bar" placeholder="search here...">
        <label for="search-bar" class="fa fa-search" ></label>
    </form>

</header>

当我尝试运行 javascript 代码时,它给出了错误。我检查了代码和语法两次,但找不到错误,请帮助我。

2个回答

您的代码中有两个问题,第一 #Search-btn id 不存在,应该是 #search-btn ,第二 searchBtn.classlist.toggle('fa-times'); 这里的 classlist 是错误的,应该是 classList ,如下面的代码:

let searchBtn = document.querySelector('#search-btn');
let searchBar = document.querySelector('.search-bar-container');

searchBtn.addEventListener('click', () =>{
 searchBtn.classList.toggle('fa-times');
 searchBar.classList.toggle('active');
});

下面的代码片段正是您仅修复了上述两个问题的代码片段:

let searchBtn = document.querySelector ( '#search-btn');
let searchBar = document.querySelector ( '.search-bar-container');

searchBtn.addEventListener('click', () =>{

    searchBtn.classList.toggle('fa-times');
    searchBar.classList.toggle('active');
});
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;300;400;600;700&display=swap');

:root {
    --orange:#ffa500; 
}

*{
    font-family: 'Nunito', sans-serif;
    margin: 0; padding: 0;
    box-sizing: border-box;
    text-transform: capitalize;
    outline: none; border: none;
    transition: all .2s linear;
    text-decoration: none; 
}

*::selection{
    background: var(--orange);
    color: #fff;
}

html{
    font-size: 62.5%;
    overflow-x: hidden;
    scroll-padding-top: 6rem;
    scroll-behavior: smooth;
}

header{
    position: fixed;
    top: 0; left: 0; right: 0;
    background: #333;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2rem 9%;
}

header .logo {
    font-size: 2rem;
    font-weight: bolder;
    color: #fff;
    text-transform: uppercase;
}

header .logo span {
    color: var(--orange);
}

header .navbar a{
    font-size: 2rem;
    color: #fff;
    margin:0 .8rem;
}

header .navbar a:hover{
    color: var(--orange);
}

header .icons i{
    font-size: 2.5rem;
    color:#fff;
    cursor: pointer;
    margin-right: 2rem;
}

header .icons i:hover{
    color: var(--orange);
}

header .search-bar-container{
    position: absolute;
    top: 100%; left: 0; right: 0;
    padding: 1.5rem 2rem;
    background: #333;
    border-top: .1rem solid rgba(225, 225, 225, .2);
    display: flex;
    align-items: center;
    z-index: 1001;
    clip-path: polygon(0 0, 100% 0, 0 0);
}

header .search-bar-container.active{
    clip-path: inherit;
}

#search-bar {
    width: 100%;
    padding: 1rem;
    text-transform: none;
    color: #333;
    font-size: 1.7rem;
    border-radius: 100rem;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

<header>
    <a href="#" class="logo"> <span>T</span>ravel</a>

    <nav class="navbar">
        <a href="#home">home</a>
        <a href="#book">book</a>
        <a href="#package">package</a>
        <a href="#services">services</a>
        <a href="#review">review</a>
        <a href="#contact">contact</a>
    </nav>

    <div class="icons">
        <i class="fa fa-search" id="search-btn"></i>
        <i class="fa fa-user" id="login-btn"></i>
    </div>

    <form action="" class="search-bar-container">
        <input type="search" id="search-bar" placeholder="search here...">
        <label for="search-bar" class="fa fa-search" ></label>
    </form>

</header>
Saeed Shamloo
2021-12-18

没有名为 #Search-btn 的东西,而是 #search-btn 。因此,您的 JavaScript 应如下所示:

let searchBtn = document.querySelector ( '#search-btn');
    let searchBar = document.querySelector ( '.search-bar-container');

    searchBtn.addEventListener('click', () =>{

        searchBtn.classlist.toggle('fa-times');
        searchBar.classList.toggle('active');
    });

接下来,必须在 classList 上使用 toggle 函数。但是,在此行中:

searchBtn.classlist.toggle('fa-times');

您可以看到您在那里调用了 classlist 。 修复该问题后,您的 JavaScript 代码应如下所示:

let searchBtn = document.querySelector ( '#search-btn');
    let searchBar = document.querySelector ( '.search-bar-container');

    searchBtn.addEventListener('click', () => {
        searchBtn.classList.toggle('fa-times', true);
        searchBar.classList.toggle('active', true);
    });
Robo
2021-12-18