如何通过单击其外部来关闭元素?
当前,我正在尝试关闭元素的样式与“ display = none”不同时。 我在控制台中遇到了一个错误,告诉我列表。有些不是一个函数,所以我可能不太了解“一些”方法。
我想要的更多信息: 鉴于我有3个列表(在列表中),当我单击外部或它的元素时,我想关闭所有列表
)
const lists = document.querySelectorAll(".list");
function closeList() {
document.addEventListener("click", () => {
if(lists.some((list) => list.style.display != "none")) {
return lists.style.display = none;
} else return;
});
};
I'm currently trying to close an element when its style is different than "display = none".
I'm having an error in the console telling me that lists. some aren't a function so I may not have understood well the "some" method.
More Infos on what I want :
Given that I have 3 lists (in lists), when I click outside of it or its elements I want to close all the lists)
Thanks in advance
const lists = document.querySelectorAll(".list");
function closeList() {
document.addEventListener("click", () => {
if(lists.some((list) => list.style.display != "none")) {
return lists.style.display = none;
} else return;
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 noreferrer“> node.contains()是否检查是否检查是否检查 event.target.target 是元素的后代和运行<强>回调如果不是:
You can use Node.contains() to check if Event.target is a descendant of element and run callback if not:
我为您建议这种方法:
或者您也可以只有一个JavaScript:
I suggest for you this method :
or you can also just a javascript :
JS QuerySelectorall()将返回Nodelist,但是Nodelist没有方法()。您可以使用以下内容:
Js querySelectorAll() will return a nodeList, but nodeList doesn't have method some(). You can use forEach() the following: