如何在 Javascript 中获取 foreach 元素的类名?
const dropdownmenu = document.querySelector(".dropdownmenu")
dropdownmenu.forEach(element => {
console.log(element.classname)
});
问题是如何在 Javascript 中获取 foreach 元素的类名?
const dropdownmenu = document.querySelector(".dropdownmenu")
dropdownmenu.forEach(element => {
console.log(element.classname)
});
Question is How can i get classname of element of foreach in Javascript ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个问题:
document.querySelector
仅选择一个元素 - 与指定选择器匹配的第一个元素。您应该使用document.querySelectorAll
来获取元素数组。.classname
应该是驼峰式 ->.className
我假设您正在尝试获取下拉菜单内的元素。您可以这样做:
如果您尝试侦听下拉菜单上所有项目的事件,请尝试将事件侦听器放在父级上,并使用
e.target
来定位单击的子级。Two issues:
document.querySelector
only selects one element - the first element that matches the specified selector. You should usedocument.querySelectorAll
to grab an array of elements..classname
should be camelcased ->.className
I'm assuming you are trying to grab the elements inside of the dropdown menu. Here's how you'd do that:
If you're trying to listen for events on the dropdown menu for all of the items, try putting the event listener right on the parent and using
e.target
to target the clicked child.