html - 如何在鼠标点击标签时关闭详细信息标签
解决方案
您可以添加一个事件侦听器click
来检查事件目标是否是标签的子标签,如果不是,则删除该open
属性:
var details = document.querySelector('details');
document.addEventListener('click', function(e){
if(!details.contains(e.target)){
details.removeAttribute('open')
}
})
details{
border:1px solid;
}
<details>
<summary>Click to open</summary>
<p>Click somewhere else (not this) to close the dropdown</p>
</details>
detail
标签,您可以使用querySelectorAll
选择所有元素并判断一个是否在标签之外被用户点击Array#some
:
var details = [...document.querySelectorAll('details')];
document.addEventListener('click', function(e){
if(details.some(f => f.contains(e.target)).length != 0){
details.forEach(f => f.removeAttribute('open'));
}
})
details{
border:1px solid;
}
<details>
<summary>Click to open</summary>
<p>Click somewhere else (not this) to close the dropdown</p>
</details>
<details>
<summary>Click to open 2</summary>
<p>Click somewhere else (not this) to close the dropdown</p>
</details>
<details>
<summary>Click to open 3</summary>
<p>Click somewhere else (not this) to close the dropdown</p>
</details>
推荐阅读
- python - 为 ndb.tasklets 输入注释
- ajax - wp_send_json* functions do a weird float values conversion
- java - 多个 JComboBox 侦听器
- python - Getting the name of a class which has a meta class
- sql - How to retrieve all user tables, corresponding columns and data types in SQL Server?
- html - 从 HTML 片段中删除任何视频
- javascript - My modal is not appearing, having difficulty targeting a button within a table using selectors
- python - 根据条件退出 Jupyter Notebook .ipynb 单元
- fluentd - 在 GKE 中实现默认的堆栈驱动程序行为
- rxjs - Creating an Observable that gets its value from a subscriptions calculation