简体   繁体   English

如何通过检查元素的子类具有特定的类名来将css属性添加到该元素的伪类中

[英]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"> &hearts; </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"> &hearts; </span> </span> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery-如果子元素具有某些CSS类,如何将CSS添加到父元素 - jQuery - How to add css to a parent element if child element has certain css class 如何定位在其父元素中具有类的子元素? - How to target a child element that has class in its parent element? 如果元素没有子元素,如何将 Class 名称添加到元素 - How to Add Class Name to Element If It Has No Children 如何通过jQuery中的类名称选择子元素? - How to select a child element by its class name in jQuery? 如何从 class 中获取 HTML 元素的 CSS 属性? - How to get a CSS property of an HTML element from its class? 如何使用jquery将css属性添加到具有特定属性的元素? - How to add css property to element has specific attribute using jquery? javascript:仅在父元素处于活动状态且子元素具有特定类的情况下,才如何向子元素添加属性? - javascript: how to add an attribute to a child element ONLY if a parent element is active AND child element has a certain class? 当元素的前一个兄弟元素具有特定的 class 时,如何设置元素的样式? - How can I style an element when its previous sibling has a particular class? JS vanilla:当子元素更改其 class 时,如何将 class 添加到父元素? - JS vanilla: how to add a class to a parent element when a child element changes its class? 如何检查.Attr元素是否具有特定类? - How to check if an element with .Attr has a particular class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM