gpt4 book ai didi

javascript - 如何选择没有类组合的元素?

转载 作者:行者123 更新时间:2023-12-02 23:50:47 24 4
gpt4 key购买 nike

我正在编写以下代码。如何仅隐藏没有 mopt[] 中指示的类的 div?

如您所见,我试图仅显示两个具有 QM 类的 div,但我的代码隐藏了所有 div

$('.A.W.B').css("background-color", "red");
let mopt = ['Q','M']
for (i = 0; i < mopt.length; i++) {
$(".box:not(:has(" + mopt[i] + "))").hide()
}
.box {
height: 20px;
background: khaki;
width: 100px;
text-align:center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box A B F R W Q">Has Q</div>
<div class="box A B F H K F">No Q</div>
<div class="box A B F W R">No Q</div>
<div class="box A B F H K F">No Q</div>
<div class="box A B F W R M">Has M</div>
<div class="box A B F H K F">No Q</div>
<div class="box A B Q F H M K F">Has Q & M</div>

最佳答案

您可以简单地使用此代码..

				
$('.A.W.B').css("background-color", "red");
$(".box").not(".Q,.M").hide();
<!DOCTYPE html>
<html>
<head>
<title>demo</title>
<style type="text/css">
.box
{
height: 20px;
background: khaki;
width: 100px;
text-align:center;
}
</style>
</head>
<body>

<div class="box A B F R W Q">Has Q</div>
<div class="box A B F H K F">No Q</div>
<div class="box A B F W R">No Q</div>
<div class="box A B F H K F">No Q</div>
<div class="box A B F W R M">Has M</div>
<div class="box A B F H K F">No Q</div>
<div class="box A B Q F H M K F">Has Q & M</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>

关于javascript - 如何选择没有类组合的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55657154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com