javascript – 如果另一个类在循环中有这个类,则Jquery添加类

$(document).ready(function() {
  if ($("#grid .media-box").hasClass("brand1")) {
    $("#grid .media-box-content").addClass("brand01")
  };
}
});

并在体循环div网格中

<div id="grid">
  <?php foreach($data as $items) { ?>
  <div class="media-box video <?php  echo $items->brand; ?> <?php  echo $items->country; ?>">
    <div class="media-box-image">
      <div data-width="240" data-height="168" data-thumbnail="gallery/thumbnails/thumb-2.jpg"></div>
      <div data-type="iframe" data-popup="https://www.youtube.com/watch?v=5guMumPFBag" title="Psico dell consecteture"></div>
      <div class="thumbnail-overlay">
        <i class="fa fa-video-camera mb-open-popup"></i>
        <a href="#"><i class="fa fa-link"></i></a>
      </div>
    </div>
    <div class="media-box-content">
      <div class="media-box-title">Psico dell consecteture</div>
      <div class="media-box-date">
        <?php echo $items->country; ?></div>
      <div class="media-box-text">Lorem ipsum dolor sitam psico.</div>
      <div class="media-box-more"> <a href="#">Read more</a> 
      </div>
    </div>
  </div>
  <?php } ?>
</div>

CSS:

.media-box {
  font-size: 13px;
}
.brand01 {
  background: blue !important;
}
.media-box-content {
  padding: 20px;
  position: relative;
  color: rgb(51, 51, 51);
  line-height: 17px;
}

上面的代码对我不起作用.

 <?php  echo $items->brand; ?> <?php  echo $items->country; ?>" >

从数据库中获取潜水2级.

最佳答案

$("#grid .media-box.brand1").find(".media-box-content").addClass("brand01")

如果条件为真,您的代码会将类brand01添加到所有.media-box-content.

点赞