[英]how to add css property to pseudoclass of an element by checking its child has particular class name
<div class="header">
<span class= "text">
<span class="icon disable">
::before
</span>
<span>
::after
</div>
I want to add css property background:transparent to psuedo class ::after of span with class name "text" by checking whether its child span has class name "disable". 我想通过检查其子跨度是否具有类名称“ disable”,将css属性background:transparent添加到类名称为“文本”的psuedo类:: after上。
Please, check out the image to understand my requirement to give me any solution. 请查看图片以了解我给我任何解决方案的要求。 is it possible do it in css?
有可能在CSS吗?
I trying this for many hours.. please,help me! 我尝试了好几个小时..请帮帮我! Thanks in advance.......... described in image
由于事先.......... 在图像描述
It's not that simple. 不是那么简单。 But you can do it simply using
jQuery hasClass()
method. 但是您可以简单地使用
jQuery hasClass()
方法来做到这一点。
HTML HTML
<div class="header">
<span class= "text">
<span class="icon disable">
Hai
</span>
<span>
</div>
JQuery JQuery的
if($('.icon').hasClass('disable')) {
$('.text').addClass('disableChild')
}
CSS CSS
.disableChild {
background-color: red;
}
Check this link codepen for working model. 检查此链接codepen的工作模型。
if ($(".text").children().hasClass("disable")) { $(".text").addClass("child-disable"); }
.text.child-disable::after { background: transparent; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="header"> <span class="text"> <span class="icon disable"> </span> </span> </div>
Or, when you have multiple elements on your page and you want make this happen for each element, you can use the following: 或者,当页面上有多个元素并且想要使每个元素都发生这种情况时,可以使用以下命令:
$(".text").each(function(i, el) { var $this = $(el); if ($this.children().hasClass("disable")) { $this.addClass("child-disabled"); } });
.text.child-disabled::after { background: transparent; } .header { background-color: #f5f5f5; width: 95%; margin: 12px auto; } .icon { display: block; text-align: center; } .icon:not(.disable) { color: red; } .text::after { content: ""; width: 100%; display: block; height: 4px; background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="header"> <span class="text"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. </p> <span class="icon disable"> ♥ </span> </span> </div> <div class="header"> <span class="text"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. </p> <span class="icon"> ♥ </span> </span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.