简体   繁体   English

我的 JavaScript 不起作用。 我怎么解决这个问题?

[英]My JavaScript doesn't work. How can I solve this problem?

I'm new to JavaScript, and I've included JS at the bottom of my HTML page.我是 JavaScript 的新手,我在 HTML 页面的底部包含了 JS。 I checked the selectors, actions and everything seems to match.我检查了选择器、动作和一切似乎都匹配。 However, when I try to use JS functionality nothing seems to happen;但是,当我尝试使用 JS 功能时,似乎什么也没有发生; as if there is no JS at all.好像根本没有JS。

Tried checking if the selectors and actions are valid.尝试检查选择器和操作是否有效。 Tried checking if I included the script tags within my tag.尝试检查我的标签中是否包含脚本标签。

const navBtn = document.getElementById("nav-btn");
const navBar = document.getElementById("navbar");
const navClose = document.getElementById("close");

navBtn.addEventListener("click", () => {
 navBar.classList.add("showNav");
});
navClose.addEventListener("click", () => {
 navBar.classList.remove("showNav");
});

.showNav {
  transform: translateX(0%);
}
.close {
  color: var(--mainWhite);
  font-size: 2.1rem;
  cursor: pointer;
}
.close:hover {
  color: var(--mainDark);
  padding-left: 0.2rem;
}

Complete project can be found here for more information: https://github.com/aleblok70/website .完整的项目可以在这里找到更多信息: https : //github.com/aleblok70/website

Desired result was to include action on click transform: translateX(0);期望的结果是包括点击转换的动作:translateX(0); actual results were that nothing happens on click.实际结果是点击后没有任何反应。

You are adding the listener to the element based on an id -您正在根据 id 将侦听器添加到元素中 -

const navClose = document.getElementById("close");

but your styling is based on a class - is this correct?但是您的样式是基于类的 - 这是正确的吗?

.close {..}

These need to be the same - so if the id is correct then it should be这些必须是相同的 - 所以如果 id 是正确的,那么它应该是

#close {
  color: var(--mainWhite);
  font-size: 2.1rem;
  cursor: pointer;
}
#close:hover {
  color: var(--mainDark);
  padding-left: 0.2rem;
}

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

相关问题 清除间隔不起作用。 问题是什么? 我该如何解决? - ClearInterval doesn't work. What is the problem? How can I solve it? 我的 Javascript AVG 计算器不工作。 我该如何解决? - My Javascript AVG Calculator doesn't work. How can i fix? setState()函数不起作用,我无法更新状态。 我怎么解决这个问题? - setState() function doesn't work and I cannot update my states. How can I solve this problem? 我无法处理JavaScript冲突。 您应该如何检查它们? - I can't get my JavaScript collisions to work. How should you check for them? 我的Javascript不起作用。不知道它是函数,我是如何调用它,我使用的CSS或者是什么 - My Javascript doesn't work. Don't know if it's the Function, how I'm calling it, the CSS I used or what 表中 Javascript 的动画不起作用。 怎么修? - Animation from Javascript in table doesn't work. How to fix? JavaScript代码不起作用。 为什么? - Javascript code doesn't work. Why? Javascript Jquery不起作用。 $(本) - Javascript Jquery doesn't work. $(this) document.getElementsByTagName 不起作用。 如何获取body的NodeList的长度? - document.getElementsByTagName doesn't work. How can I get the length of NodeList of body? React-router 不起作用。 我该如何解决? - React-router doesn't work. How can i fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM