javascript - 单击时选择一个下拉菜单,而不是多个
问题描述
我有多个带有砖石的卡片,并且在单击图标时添加了一个下拉菜单。当我点击图标时,每张卡片都会显示下拉菜单,或者我只想要我点击的那个。
HTML:
<div class="dropdown">
<i class="fas fa-share-alt-square social-menu"></i>
<div class="dropdown-content">
<a href="https://www.facebook.com"></i>Facebook</a>
<a href="https://www.facebook.com"></i>Facebook</a>
</div>
JS:
$('.social-menu').click(function() {
$('.dropdown-content').toggleClass('show');
});
$(document).click(function(event) {
if (!$(event.target).closest('.social-menu').length) {
if ($('.dropdown-content').is(":visible")) {
$('.dropdown-content').toggleClass('show');
}
}
});
解决方案
你真的不需要 2click
处理程序来做到这一点。
您唯一需要记住的是,您必须show
真正dropdown-content
接近social-menu
被点击。考虑到您的HTML
结构,您可以这样做:
$('.social-menu').click(function() {
var parentDiv = $(this).closest(".dropdown"); //find the parent div holding <i> as well as dropdown-content
parentDiv.find('.dropdown-content').toggleClass('show'); //target the exact dropdown-content
});
推荐阅读
- excel - 后台加载 Excel 使用任务管理器加载数据并上传到 salesforce
- string - 在循环的每个步骤中更改文件名(索引)
- excel - 如何将spss输出的唯一手段和sds放入excel?
- python - 无法将自定义 Python 宏导入 Airflow 2
- spring-boot - 如何使用 Spring RestTemplate 为 POST 请求设置超时?
- swift - 如何跳过 NSCollectionView 中的第一个单元格?
- python - 如何在循环python中使用子字符串制作字典
- ios - IOS UIKit 触摸/移动延迟
- c++ - 将值存储在从文件 c++98 读取的对象的数组或向量中
- reactjs - 如何在 React 中使用刷新令牌保持刷新令牌