简体   繁体   English

仅将类添加到一个div

[英]Adding class to only one div

I need some help adding a class to a div if is has an image to it. 如果有一个图像,我需要一些帮助添加一个类到div。

<div class="large-6 columns check-Div">
  <div class="custom-table">
    <div class="text">
      <?php echo $latestimage; ?>
    </div>
    <div class="custom-table-box">
      <?php echo $table; ?>
    </div>
  </div><!-- end custom-table -->
</div>
if ($j(".large-6.columns.check-Div > .custom-table-box:contains('size-full')")) {
  $j('.large-6.columns.check-Div').addClass("hasImage");
}

The jQuery above is adding the hasImage class to all of the divs but I only want it to apply when there is a div that contains a image. 上面的jQuery是将hasImage类添加到所有div,但我只希望它在包含图像的div时应用。 Is there a way to do it singularly? 有没有办法单独做到这一点? Or by parent? 还是由父母? I have tried but I'm a little lost. 我试过了,但我有点失落。

Any help would be wonderful! 任何帮助都会很精彩!

CSS :has selector is what you are looking for (here for a direct descendant) CSS 选择器是你正在寻找的(这里是直接后代)

$('div:has(> img)').addClass("hasImage") //jquery way

or just in css (but this would require a polyfill , as its in Level 4, which is draft currently) 或者只是在css中(但这需要一个polyfill ,因为它在Level 4中,当前是草稿)

div:has(> img) {
   border-color: 1px solid red;
}

https://developer.mozilla.org/en-US/docs/Web/CSS/:has https://developer.mozilla.org/en-US/docs/Web/CSS/:has

JSfiddle here: https://jsfiddle.net/y6gtt2vg/1/ JSfiddlehttps//jsfiddle.net/y6gtt2vg/1/


Browser support 浏览器支持

About CSS Selector Compatibility 关于CSS选择器兼容性

Regardless of a browser's support of CSS selectors, all selectors listed at api.jquery.com/category/selectors/ will return the correct set of elements when passed as an argument of the jQuery function. 无论浏览器支持CSS选择器,api.jquery.com/category/selectors/中列出的所有选择器在作为jQuery函数的参数传递时都将返回正确的元素集。

https://jquery.com/browser-support/ https://jquery.com/browser-support/

您可以通过删除if语句并仅根据:has选择器添加类来完成此操作:

$j('.large-6.columns.check-Div:has(img)').addClass('hasImage');

您可以使用jquery执行此操作,无需添加if条件

jQuery(".custom-table-box:has(img)").parents(".check-Div").addClass("hasImage");

For clarity I have added additional background color 为清楚起见,我添加了额外的背景颜色

 $(".custom-table").children("div").each(function(item,index) { $(this).has("img").css("background-color", "red"); // Comment it if not needed $(this).has("img").addClass("hasImage"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="large-6 columns check-Div"> <div class="custom-table"> <div class="text"> xyz<img /> </div> <div class="custom-table-box"> abc </div> </div> </div> 

试试这个代码

$('.large-6 .columns .check-Div div:has(img)').addClass('hasImage');

I think you should try using id. 我想你应该尝试使用id。

<div id="addImg" class="large-6 columns check-Div">
                  <div class="custom-table">
                  <div class="text">
                    <?php echo $latestimage; ?>
                  </div>


                    <div class="custom-table-box">
                      <?php echo $table; ?>
                    </div>
                  </div><!-- end custom-table -->
                </div>

if ($j(".large-6.columns.check-Div > .custom-table-box:contains('size-full')")) { $j('#addImg').addClass(".hasImage");//also missing . for class }

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

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