admin管理员组文章数量:1287960
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;
});
};
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;
});
};
Share
Improve this question
edited Jun 20, 2022 at 20:15
Raju Ahmed
1,2685 gold badges15 silver badges24 bronze badges
asked Jun 20, 2022 at 8:17
user18845463user18845463
2
- Please share your Html code so. – Raju Ahmed Commented Jun 20, 2022 at 8:22
- you should run the click event outside the function. And find out if the click is on or inside your .list element, else close all lists. – Medda86 Commented Jun 20, 2022 at 8:23
3 Answers
Reset to default 7You can use Node.contains() to check if Event.target is a descendant of element and run callback if not:
function onClickOutside(ele, cb) {
document.addEventListener('click', event => {
if (!ele.contains(event.target)) cb();
});
};
// Using
onClickOutside('#list', () => console.log('Hi!'));
// Will log 'Hi!' whenever the user clicks outside of #list
I suggest for you this method :
@HostListener('document:click', ['$event.target'])
public onClick(targetElement) {
const event = this.elementRef.nativeElement.contains(targetElement);
if (targetElement && !event) {
if (this.isClickedOutside) {
// this.shownInfo = false;
}
}
or you can also just a javascript :
window.addEventListener('click', function(e){
if (document.getElementById('clickbox').contains(e.target)){
// Clicked in box
} else{
// Clicked outside the box
}
});
Js querySelectorAll() will return a nodeList, but nodeList doesn't have method some(). You can use forEach() the following:
const lists = document.querySelectorAll(".list");
function closeList() {
document.addEventListener("click", () => {
lists.forEach(list => {
if(list.style.display != "none") lists.style.display = none
})
});
};
本文标签: javascriptHow to close an element by clicking outside of itStack Overflow
版权声明:本文标题:javascript - How to close an element by clicking outside of it? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741325499a2372454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论