简体   繁体   English

如何修复Javascript中的下拉菜单?

[英]How do I fix my dropdown menu in Javascript?

I am making a navbar for my project which has several dropdown menus, here is my navbar code: 我正在为我的项目创建一个导航栏,其中有几个下拉菜单,这是我的导航栏代码:

<nav>
    <a class="logo" href="{% url 'home' %}">LOGO</a>

    <div class="navbar">
        <div class="dropdown">
            <a href="#" class="dropdown-btn" id="dropdown-btn">Teacher <i class="fa fa-caret-down"></i> </a>
            <div class="dropdown-content">
                <a href="{% url 'teacher:add-teacher' %}">Add Teacher</a>
                <a href="{% url 'teacher:list-teacher' %}">List Teacher</a>
            </div>
        </div>
        <div class="dropdown">
            <a href="#" class="dropdown-btn" id="dropdown-btn">Blog <i class="fa fa-caret-down"></i> </a>
            <div class="dropdown-content">
                <a href="{% url 'blog:blog-form' %}">Add Blog</a>
                <a href="#">Blog List</a>
            </div>
        </div>
    </div>
</nav>

I want that onclick of dropdown-btn, it shows dropdown-content. 我想点击dropdown-btn,它显示下拉内容。 I tried below code, but it gives the following error in console. 我尝试下面的代码,但它在控制台中给出以下错误。

Uncaught TypeError: Cannot read property 'classList' of undefined 未捕获的TypeError:无法读取未定义的属性'classList'

and this is my javascript Code. 这是我的JavaScript代码。 .dropdown-content has a display of none and active-dropdown has display:block in it .dropdown-content显示为none, active-dropdown display:block in it

let i, dropdownBtns = document.getElementsByClassName("dropdown-btn");
for (i = 0; i < dropdownBtns.length; i++) {
    dropdownBtns[i].addEventListener('click', () => {
        let panel = this.nextElementSibling;
        panel.classList.toggle('active-dropdown');
    });
}

Thank you in advance! 先感谢您!

Two issues: 两个问题:

1) Your loop was using an i that was outside of its scope 1)你的循环正在使用超出其范围的i

2) You were using the new () => {} fat arrow function syntax which doesn't bind this to the function - so you were referring to the window when you wrote this.nextElementSibling . 2)您使用的是new () => {}胖箭头函数语法,它不会this函数绑定到函数 - 所以当您编写this.nextElementSibling时,您指的是window

Try this: 尝试这个:

var dropdownBtns = document.getElementsByClassName("dropdown-btn");

for (var i = 0; i < dropdownBtns.length; i++) {
    dropdownBtns[i].addEventListener('click', function () {
        let panel = this.nextElementSibling;
        panel.classList.toggle('active-dropdown');
    });
}

Your issue is that your arrow function was using a different this - also, look at your scoping for i : 你的问题是,你的箭功能使用不同的就是this -也是,看看你的范围界定为i

var dropdownBtns = document.getElementsByClassName("dropdown-btn");

for (let i = 0; i < dropdownBtns.length; i++) {
    dropdownBtns[i].addEventListener('click', function() {
        let panel = this.nextElementSibling;
        panel.classList.toggle('active-dropdown');
    });
}

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

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