如何创建条件检查以执行特定操作?例如:
<h1 class = "parentclass1">
<p class = "childclass1"></p> <!--- This child class is conditional--->
</h1>
<h1 class = "parentclass2">
<p class = "childclass2"></p>
</h1>仅当"parentclass1“具有"childclass1,否则忽略它”时,我才想更改"childclass2“的文本颜色。(以下代码的父类名称为id name,而不是class )
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
if($("h5").parent().find("hide")){
#price-field .money {color:red};
}
</script>
</head>
<body>
<h5 class="compare-at-price js" style="font-size: 14px;">
<span class="money" style="font-size: 14px;">$60</span>
</h5>
<h3 class="custom-font" id="price-field" style="font-size: 24px;">
<span class="money" style="font-size: 24px;">$50</span>
</h3>
<h5 class="compare-at-price js hide" style="font-size: 14px;">
</h5>
<h3 class="custom-font" id="price-field" style="font-size: 24px;">
<span class="money" style="font-size: 24px;">$70</span>
</h3>
</body>
</html>发布于 2021-11-21 08:27:20
对子元素使用hasClass函数。
if($(".parentclass1").children("p").hasClass("childclass1")) {
$(".childclass2").css('color', 'red');
}https://stackoverflow.com/questions/70052506
复制相似问题