如何从与scripts.js文件不同的文件夹中的nodejs文件中读取HTML className?
我有一个带有SRC文件夹HTML的前端文件夹,html,css script.js文件以及我的nodejs文件。在我的src文件夹中,我有一个称为组件的文件夹,在那里有一个用于我的navbar的组件标头:
const Header = {
render: () => {
const { firstname, isAdmin} = getCustomerInfo();
return `
<div class="container">
<nav>
<div class="Rlogo"><h4>At Home</h4></div>
<ul class="nav-links">
<li><a href="/#home">Home</a></li>
<li><a href="/#about">About Us</a></li>
<li><a href="/#menu">Menu</a></li>
<li><a href="/#resturant">Resturant</a></li>
${
firstname
? `<li><a href="/#/profile">${firstname}</a></li>`
: `<li><a href="/#/signin">Sign-In</a></li>`
}
<li><a href="/#/cart">Cart</a></li>
${isAdmin?`<li><a href="/#/dashboard"> Dashboard</a></li>`: ''}
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
</div>
`;
},
after_render: () => {},
};
export default Header;
我的脚本中有一个函数navslide.js.js文件:
const navSlide = () =>{
const burger = document.querySelector('.burger');
const nav = document.querySelector('.nav-links');
const navLinks = document.querySelectorAll('.nav-links li');
// toggle Nav
burger.addEventListener('click', () =>{
nav.classList.toggle('nav-active');
// Animate links
navLinks.forEach((link, index) => {
if(link.style.animation ){
link.style.animation = ''
}else{
link.style.animation = `navLinkFade 0.5 ease forwards ${index/7 + 2.0}s`
}
});
// burger animation
burger.classList.toggle('toggle');
})
}
navSlide();
我已经在末尾链接了这样的脚本在这样的闭合车身标签中,但是在我的控制台中,我会遇到错误:无法读取null的属性(读取'addeventListener'):
<script src="./scripts.js"></script>
<script src="main.js"></script>
</body>
</html>
我知道脚本无法找到.navlinks,burger class由于它们不在HTML文件中,而是在标题中的组件文件夹中。感谢您的任何指导和帮助
I have a front-end folder with a src folder, HTML, Css an Script.js file along with my nodejs files. In my src folder I have a folder called components and in there there is a component header for my navbar like this:
const Header = {
render: () => {
const { firstname, isAdmin} = getCustomerInfo();
return `
<div class="container">
<nav>
<div class="Rlogo"><h4>At Home</h4></div>
<ul class="nav-links">
<li><a href="/#home">Home</a></li>
<li><a href="/#about">About Us</a></li>
<li><a href="/#menu">Menu</a></li>
<li><a href="/#resturant">Resturant</a></li>
${
firstname
? `<li><a href="/#/profile">${firstname}</a></li>`
: `<li><a href="/#/signin">Sign-In</a></li>`
}
<li><a href="/#/cart">Cart</a></li>
${isAdmin?`<li><a href="/#/dashboard"> Dashboard</a></li>`: ''}
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
</div>
`;
},
after_render: () => {},
};
export default Header;
I have a function navslide in my script.js file like this:
const navSlide = () =>{
const burger = document.querySelector('.burger');
const nav = document.querySelector('.nav-links');
const navLinks = document.querySelectorAll('.nav-links li');
// toggle Nav
burger.addEventListener('click', () =>{
nav.classList.toggle('nav-active');
// Animate links
navLinks.forEach((link, index) => {
if(link.style.animation ){
link.style.animation = ''
}else{
link.style.animation = `navLinkFade 0.5 ease forwards ${index/7 + 2.0}s`
}
});
// burger animation
burger.classList.toggle('toggle');
})
}
navSlide();
I have linked my script like this at right before the end of the closing body tag like this but in my console I´m getting the error: Cannot read properties of null (reading 'addEventListener'):
<script src="./scripts.js"></script>
<script src="main.js"></script>
</body>
</html>
I´m aware that the script isn't able to find the .navlinks, .burger classes as they´re not in the HTML file but in the component folder in the header.js file my question is how can I make the script read the class so its not returned as null cause I want to have a menu for the responsive design. Thank you for any guidance and help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论