提问人:user9867610 提问时间:11/21/2021 更新时间:11/21/2021 访问量:188
更改另一个类的同级的子类的文本颜色
changing the text color of the child class of a sibling of another class
问:
如何创建条件检查来执行某个操作?前任:
<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>
答:
0赞
sid
11/21/2021
#1
在子元素上使用 function。hasClass
if($(".parentclass1").children("p").hasClass("childclass1")) {
$(".childclass2").css('color', 'red');
}
编号: https://api.jquery.com/hasclass/
评论
0赞
user9867610
11/22/2021
我按照你的建议做了,但没有运气。也许我错过了什么:<head> <script src=“ajax.googleapis.com/ajax/libs/jquery/3.5.1/...> <script> if($(“.compare-at-price”).children(“span”).hasClass(“.money”)) { $(“.money”).css('color', 'red'); } </script> </head>
0赞
sid
11/22/2021
jQuery 和相关脚本应该放在 </html> 标签之后
评论