gpt4 book ai didi

javascript - 如何编辑不包含 H1 标签的 div 的 CSS?

转载 作者:行者123 更新时间:2023-11-29 16:10:19 24 4
gpt4 key购买 nike

有两个div:

<div class="test">
<h1>This is a test</h1>
<p>Welcome to the test</p>
</div>

<div class="test">
<p>Welcome to the test</p>
</div>

如您所见,有两个属于同一类的 div,但是一个 div 有 H1 标签,另一个没有。

CSS:

.test {
font-size: 20px;
color: #fff
}

上面显示了 div test 的 CSS。

我想做的是将额外的 CSS 应用到不包含 H1 标签的 div。

如果 div 包含 H1 标签:

  • 什么都不做

如果 div 不包含 H1 标签:

  • 将以下 CSS 应用于该特定的 div

字体大小:30px;

最佳答案

$('.test:not(:has(h1))').css('font-size', '30px');
.test {
font-size: 20px;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="test">
<h1>This is a test</h1>
<p>Welcome to the test</p>
</div>

<div class="test">
<p>Welcome to the test</p>
</div>

您可以将选择器与 :not 一起使用和 :has喜欢

$('.test:not(:has(h1))').css('font-size', '30px');

关于javascript - 如何编辑不包含 H1 标签的 div 的 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30385184/

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