jquery - 显示和隐藏
问题描述
let opened = false;
function turnNestedlist(opened)
{
elem = countImg;
$(`.list__ul .nested-list.num-${elem}`).click(function(event)
{
if (!opened){
$(this).addClass('active');
$(`.list__ul .nested-list.num-${elem}.active li`).show("fast");
opened = true;
}
$(this).click(function(event)
{
if (opened){
$(this).removeClass('active');
$(`.list__ul .nested-list.num-${elem} li`).hide("fast");
opened = false;
}
});
});
}
$(`.list__ul .nested-list.num-${countImg}`).click(turnNestedlist(opened));
任务是,当您单击每个列表时ul
,会打开该元素的 4 个嵌套元素li
。此代码有效,但对于每个ul
元素仅一次,即:第一次单击发生 - 列表关闭,第二次单击打开,第三次单击 - 列表立即打开和关闭。
解决方案
推荐阅读
- vue.js - VueJS 和 NuxtJS 中是否有类似 React Fragment 的东西?
- c# - 使用 UploadFromStreamAsync 上传文件后 Azure Blob 文件已损坏
- laravel - 为什么 laravel 消息包中有两个相同的方法?
- python - Shopify 最新 sdk 版本
- c - 使用 strcpy 到 calloc'ed 位置时,大小为 1 的写入无效
- javascript - React 中的脚本加载问题
- assembly - 数组的 MIPS 指令 sll
- jquery - 如何将相同的值收集到 JSON 对象中?
- frequency - 在 3 种不同的实验条件下 y 发生后 x 出现的频率
- c# - 将 JSON 对象绑定到 ASP.NET Core 中的列表