简体   繁体   English

为什么切换在 Firefox 开发人员工具上不起作用?

[英]Why toggling doesn't work on Firefox Developer Tools?

I don't know why my javascript code is not toggling on firefox developers tools, and that's my code:我不知道为什么我的 javascript 代码没有在 firefox 开发人员工具上切换,这就是我的代码:

const hamburger = document.querySelector('.header .nav-bar .nav-list .hamburger');
const mobile_menu = document.querySelector('.header .nav-bar .nav-list .menu');
const header = document.querySelector('.header-container')

hamburger.addEventListener('click',() => {
    hamburger.classList.toggle('active');
});

try document.querySelectorAll();尝试document.querySelectorAll(); ? ?

Maybe you can check, if your element is clicked or not with console.log()也许您可以使用 console.log() 检查您的元素是否被点击

hamburger.addEventListener('click', function(){
console.log("test")
})

I suggest you not to use arrow function in addEventListener, because of 'this' problem in arrow function我建议您不要在 addEventListener 中使用箭头 function,因为箭头 function 中的“this”问题

When you open developer tools in Firefox, on the left hand side of the menu there are tab header for "inspect", "console", network" and so on.当您在 Firefox 中打开开发人员工具时,在菜单的左侧有选项卡 header 用于“检查”、“控制台”、网络等。

On the right hand side of the same menu bar, there is an icon of a framed document that shows "Select an iframe as the currently targeted document" when you hover over it.在同一菜单栏的右侧,有一个带框文档的图标,当您在其上方放置 hover 时,该图标显示“选择 iframe 作为当前目标文档”。 Click the icon and select the iframe containing the .header-container element.单击图标和 select iframe 包含.header-container元素。

Assuming the correct nested elements have been set up in HTML, the posted code now runs if you paste it after the script input prompt and press enter, and you can click the hamburger icon to toggle its active cf class.假设在 HTML 中设置了正确的嵌套元素,如果您在脚本输入提示后粘贴并按 Enter 键,则发布的代码现在运行,您可以单击汉堡图标来切换其active cf class。

Use of the const declaration in the pasted script prevents it being run more than once without reloading the page, which is a good thing - an even number of anonymous listeners that each toggle active would not affect the class list of the hamburger element.在粘贴的脚本中使用const声明可以防止它在不重新加载页面的情况下多次运行,这是一件好事 - 每个切换active的偶数匿名侦听器不会影响汉堡元素的 class 列表。

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

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