addEventListener为null

时间:2018-12-29 13:50:31

标签: javascript html css

因此,我正在创建一个响应式导航栏。

nav bar when on big screen

当用户使用电话或最小化浏览器时,汉堡包图标应如下所示:

navbar hamburger 因此,我创建了一个js函数,每当用户单击该图标时,他/她都应该看到所有可用的链接。但是当我在浏览器中检查控制台时,它说addEventListener为null。我不认为我的函数定义有误,并再次检查文档以查看是否写错了代码,但这不是那样。

HTML

 <nav class="navbar">
            <span class="navbar-toggle" id="js-navbar-toggle">
                <i class="fas fa-bars"></i>
            </span>
            <a href="#" class="logo">logo</a>
            <ul class="main-nav" id="js-menu">
                <li>
                    <a href="#" class="nav-links">Home</a>
                </li>
                <li>
                    <a href="#" class="nav-links">Products</a>
                </li>
                <li>
                    <a href="#" class="nav-links">About Us</a>
                </li>
                <li>
                    <a href="#" class="nav-links">Contact Us</a>
                </li>
                <li>
                    <a href="#" class="nav-links">Blog</a>
                </li>
            </ul>
    </nav>

CSS

    * {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}

body {
    font-family: 'Josefin Sans', sans-serif;
}

.navbar {
    font-size: 18px;
    background-image: linear-gradient(260deg, #2376ae 0%, #c16ecf 100%);
    border: 1px solid rgba(0, 0, 0, 0.2);
    padding-bottom: 10px;
}

.main-nav {
    list-style-type: none;
    display: none;
}

.nav-links,
.logo {
    text-decoration: none;
    color: rgba(255, 255, 255, 0.7);
}

.main-nav li {
    text-align: center;
    margin: 15px auto;
}

.logo {
    display: inline-block;
    font-size: 22px;
    margin-top: 10px;
    margin-left: 20px;
}

.navbar-toggle {
    position: absolute;
    top: 10px;
    right: 20px;
    cursor: pointer;
    color: rgba(255, 255, 255, 0.8);
    font-size: 24px;
}

.active {
    display: block;
}

@media screen and (min-width: 768px) {
    .navbar {
        display: flex;
        justify-content: space-between;
        padding-bottom: 0;
        height: 70px;
        align-items: center;
    }

    .main-nav {
        display: flex;
        margin-right: 30px;
        flex-direction: row;
        justify-content: flex-end;
    }

    .main-nav li {
        margin: 0;
    }

    .nav-links {
        margin-left: 40px;
    }

    .logo {
        margin-top: 0;
    }

    .navbar-toggle {
        display: none;
    }

    .logo:hover,
    .nav-links:hover {
        color: rgba(255, 255, 255, 1);


  }
}

和JS:

let mainNav = document.getElementById('js-menu');
let navBarToggle = document.getElementById('js-navbar-toggle');

navBarToggle.addEventListener('click', function () {
  mainNav.classList.toggle('active');
});

是说Uncaught TypeError: Cannot read property 'addEventListener' of null

预期的结果将使单击汉堡包图标时显示链接。

1 个答案:

答案 0 :(得分:2)

尝试将代码添加到文档加载中。当您尝试绑定单击事件时,您的dom还没有准备好,因此这就是您的navBarToggle为null的原因。

import 'package:flutter/material.dart';
// Subclass of DropdownButton based on String only values.
// Yes, I know Flutter discourages subclassing, but this seems to be
// a reasonable exception where a commonly used specialization can be
// made more easily usable.
//
// Usage: 
// DropdownStringButton(items: ['A', 'B', 'C'], value: 'A', onChanged: (string) {})
//
class DropdownStringButton extends DropdownButton<String> {
  DropdownStringButton({
    Key key, @required List<String> items, value, hint, disabledHint,
    @required onChanged, elevation = 8, style, iconSize = 24.0, isDense = false,
    isExpanded = false, }) : 
    assert(items == null || value == null || items.where((String item) => item == value).length == 1),
        super(
          key: key,
          items: items.map((String item) {
            return DropdownMenuItem<String>(child: Text(item), value: item);
          }).toList(),
        value: value, hint: hint, disabledHint: disabledHint, onChanged: onChanged,
        elevation: elevation, style: style, iconSize: iconSize, isDense: isDense,
        isExpanded: isExpanded,
        );
    }