简体   繁体   English

如果找到类,如何为特定的Div添加CSS属性(jQuery)

[英]How do I add css property for particular Div if class is found(JQuery)

<div class="b" >
   <h1>Hello</h1>
   <div class="a"> 
     <p class="ABC">A........Z</p> //this could be present in some pages
   </div>
</div>

This is a piece of code in which I want to add css properties to div with class "b" if <p> contains class="ABC" . 这是一段代码,如果<p>包含class="ABC" ,我想在其中添加css属性到类为“ b”的div中。 How to do it? 怎么做?

 $("p.ABC").parents("div.b").css('background-color', 'red'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="b" > <h1>Hello</h1> <div class="a"> <p class="ABC">A........Z</p> //this could be present in some pages </div> </div> 

  • $("p.ABC") this finds all the p elements has the class ABC . $("p.ABC")发现所有p个元素都具有类ABC
  • parents("div.b") finds the first parent that is div and has class named b of the selected element. parents("div.b")查找div的第一个父级,该父级具有所选元素的名为b的类。
  • css() adds the styles you want. css()添加所需的样式。 You can also use addClass() method to add predefined class. 您也可以使用addClass()方法添加预定义的类。

Please check if this helps 请检查是否有帮助

if($( "p" ).hasClass( "ABC" )) {
// if you want to add a class with many properties, then
$( "div.b" ).addClass( "yourClass" );
// if you want to add one property to existing class then the below statement
$( "div.b" ).css( "attribute-name", value );
}

You can add all the css properties in "yourClass" 您可以在“ yourClass”中添加所有css属性

 if($("p").hasClass("ABC")) { $("div.b").css("color", "blue"); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="b" > <h1>Hello</h1> <div class="a"> <p class="ABC">A........Z</p> //this could be present in some pages </div> </div> 

Please check the code snippet. 请检查代码段。

I added a css property color as blue to the class 'b' if 'p' has 'ABC'. 如果“ p”具有“ ABC”,则将蓝色的css属性颜色添加到类“ b”中。 its working 它的工作

You can use $("p.ABC") to find all <p> with class ABC then .closest(".b") to find the parent div with class b then .css() to change css properties. 您可以使用$("p.ABC")查找所有ABC类的<p> ,然后使用.closest(".b")查找具有b类的父div,然后使用.css()来更改css属性。

I recommend using .addClass() / .removeClass() rather than change css directly, but this helps you find the parent. 我建议使用.addClass() / .removeClass()而不是直接更改CSS,但这可以帮助您找到父项。

This also allows you to have multiple div class='b' in your html and it will only apply to the one with ABC 这还允许您在HTML中具有多个div class='b' ,并且仅适用于具有ABC

 $("p.ABC").closest(".b").css("background-color", "pink"); 
 .b+.b { border-top: 1px solid black; margin-top: 5px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="b"> <h1>Hello</h1> <div class="a"> <p class="ABC">A........Z</p> //this could be present in some pages </div> </div> <div class="b"> <h1>Hello</h1> <div class="a"> <p class="DEF">ABC not present</p> </div> </div> 


You can also turn it around using :has 您也可以使用:has扭转它

$(".b:has(.ABC)").css("background-color", "pink")

which states: select all .b that has at least one child that has class .ABC , which is nearer to your concept of: add css to div with class "b" if <p> contains class="ABC" 其中指出:选择至少有一个子级为.ABC所有.b ,这与您的概念更接近:如果<p>包含class="ABC" ,则将CSS添加到类为“ b”的div中

So it depends on which way you want to work it. 因此,这取决于您要使用哪种方式。

 $(".b:has(.ABC)").css("background-color", "pink") 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="b"> <h1>Hello</h1> <div class="a"> <p class="ABC">A........Z</p> //this could be present in some pages </div> </div> <div class="b"> <h1>Hello</h1> <div class="a"> <p class="DEF">ABC not present</p> </div> </div> 

One method is to look directly at <p class="ABC"> and check if it has a class using hasClass() . 一种方法是直接查看<p class="ABC">并使用hasClass()检查它是否具有一个类。 Then you can traverse upwards to find the nearest element with class="b" 然后,您可以向上遍历以找到最近的class="b"元素

Something like this: 像这样:

var elementToModify = $('p').hasClass('ABC').closest('.b');
elementToModify.prop('property', newValue);

Checkout these to further understand their use: 查看这些内容以进一步了解其用法:

prop() 支柱()

closest() 最近()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM