jquery - Jquery - “hasClass()”不起作用
问题描述
嘿嘿,我有这样的代码
HTML
<div class="card">
<a href="#!">Click</a>
<ul class="overlay_modal">
/*some LI element*/
</ul>
</div>
CSS
.active {
opacity: 1 !important;
visibility: visible !important;
}
.overlay_modal {
visibility: hidden;
opacity: 0;
left: 0;
top: 0;
width: 100%;
height: 100%;
position: fixed;
background-color: rgba(0, 0, 0, 0.56);
}
jQuery
$("div.card > a").click(function() {
$(this).next().addClass("active");
});
if ($(".overlay_modal").hasClass("active")) {
$("body").css({
"height" : "100vh",
"overflow" : "hidden"
});
}
else {
$("body").css("background-color", "red");
}
结果
如您所见,当hass class时,body
背景仍然是红色的。我的代码有什么问题,请帮助我:).overlay_modal
active
解决方案
像这样向你的 ul 元素添加一个活动类:
<ul class="option__list active">
您拥有的 jquery 表达式会否定,因为首先没有 .active 类。
编辑:
$("div.card .option > a").click(function() {
$(this).next().addClass("active");
if ($(".card .option ul").hasClass("active")) {
$("body").css({
"height" : "100vh",
"overflow" : "hidden"
});
}
else {
$("body").css("background-color", "red");
}
});
推荐阅读
- java - 我只想从 firebase 中删除收藏夹
- excel - Excel宏:在命名行之间,如果另一个单元格=“c”,则单元格等于零
- clojure - 将文件行的惰性序列传递给函数
- r - 来自成对比较 p 值的紧凑型字母显示 (CLD)
- javascript - 循环遍历动态对象:signalData[signals].data 不是函数
- postgresql - 如何将标准格式时区转换为 tz 数据库时间格式?
- sql - d6 增量查询 - 获取最新条目
- c++ - 带有记录器策略的模板类,用于在编译时启用或禁用日志记录
- c++ - 什么是 C++ 中的“进程被杀死”?
- c# - 我想将计算机视觉 api 结果发布到网页中