当div为空时隐藏div并将class添加到另一个div

时间:2017-09-06 08:44:41

标签: javascript jquery

我们有产品价格和销售价格

两个类都有宽度:50%和内联块。

我想编写一个脚本,在没有销售价格时更改样式(将宽度100%的类添加并更改字体样式)。

这是我到目前为止所尝试的。

这是第一个代码:

jQuery(document).ready(function($) {
 if( $('.eg-top-ponudbe-content-element-28').html().trim().length === 0) {
     $('.eg-top-ponudbe-content-element-28').hide();
     $('.eg-top-ponudbe-content-element-24').addClass('change_regular_price');
 }
});

这是第二个代码:

jQuery(document).ready(function($) {
 if( $('.eg-top-ponudbe-content-element-28').is(':empty')) {
    $('.eg-top-ponudbe-content-element-28').hide();
    $('.eg-top-ponudbe-content-element-24').addClass('change_regular_price');
 }
});

并且也不起作用,因为隐藏所有产品的销售价格(也是销售价格的产品)

HTML是:

<div class="price_div">   
 <div class="esg-content eg-post-903 eg-top-ponudbe-content-element-28-a">
   <a class="eg-top-ponudbe-content-element-28 eg-post-903"  href="javascript:void(0);" target="_self">regular_price 1.200 €</a></div>

<div class="esg-content eg-post-903 eg-top-ponudbe-content-element-24-a">
  <a class="eg-top-ponudbe-content-element-24 eg-post-903"   href="javascript:void(0);" target="_self">sale_price 1.100 €</a></div>

  </div>
 </div>

3 个答案:

答案 0 :(得分:0)

您必须遍历每个price_div,并在其中找到常规和销售对,然后您可以独立于其他价格div更新它们。事情路线:

int v;           // we want to find the absolute value of v
unsigned int r;  // the result goes here 
unsigned int mask = -((unsigned int)v >> (sizeof(unsigned int) * CHAR_BIT - 1));

r = ((unsigned int)v + mask) ^ mask;

答案 1 :(得分:0)

您可以使用jQuery选择$(&#39; .sale_price:空&#39;)的空sale_price商品,然后通过添加.each()

来遍历结果

&#13;
&#13;
name = "jhjh"
&#13;
$('.sale_price:empty').each(function() {
  $(this).hide();
  $(this).parent().find('.regular_price').addClass('change_regular_price');
})
&#13;
.sale_price {
  width: 50%;
  background: red;
  float: left;
}

.regular_price {
  width: 50%;
  background: blue;
  float: left;
}

.regular_price.change_regular_price {
  width: 100%;
  background: green;
  float: left;
}

ul {
  list-style: none;
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

jQuery(document).ready(function($) {
  $( ".price_div" ).each(function() {
    var regular_price = $(this).find('div:first-child');
    var sale_price = $(this).find('div:last-child');
    if( sale_price.find('a').html().trim().length === 0) {
      sale_price.hide();
      regular_price.addClass('change_regular_price');
    } 
  });
});
.price_div {
          margin-bottom: 20px;
          width: 600px;
          height: 40px;
          border: 1px solid #ccc;
        }
        .price_div div {
          display: inline-block;
          width: 50%;
          height: 40px;
          line-height: 40px;
          float: left;
          text-align: center;
        }
        .change_regular_price {
          width: 100% !important;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="price_div">   
  <div class="esg-content eg-post-903 eg-top-ponudbe-content-element-28-a">
    <a class="eg-top-ponudbe-content-element-28 eg-post-903" href="javascript:void(0);" target="_self">regular_price 1.200 €</a>
  </div>
  <div class="esg-content eg-post-903 eg-top-ponudbe-content-element-24-a">
    <a class="eg-top-ponudbe-content-element-24 eg-post-903" href="javascript:void(0);" target="_self">sale_price 1.100 €</a>
  </div>
</div>
<div class="price_div">   
  <div class="esg-content eg-post-903 eg-top-ponudbe-content-element-28-a">
    <a class="eg-top-ponudbe-content-element-28 eg-post-903" href="javascript:void(0);" target="_self">regular_price 1.200 €</a>
  </div>
  <div class="esg-content eg-post-903 eg-top-ponudbe-content-element-24-a">
    <a class="eg-top-ponudbe-content-element-24 eg-post-903" href="javascript:void(0);" target="_self"></a>
  </div>
</div>