[英]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.