如何使用jquery或javascript隐藏div?

时间:2014-10-31 05:50:30

标签: javascript jquery html css hide

你好我想隐藏一个span和一个div,如果值是0.00 ..我的代码是

<span class="price-old"> <%=getCurrentAttribute('item','custitem_sumisho_listprice')%>      </span>
<span class="price-new"><%=getCurrentAttribute('item','custitem_sumisho_onlineprice')%>  </span>
<div class="save-sale" style="font-size: .8em; padding-top: 4em"><%=getCurrentAttribute('item','custitem_sumisho_totalsave')%></div>

在上面的代码中,如果class =“save-sale”的div将有0.00值,那么我想隐藏class =“price-old”和class =“save-sale”。这里getattribute标签会得到商品价格的价值。

我已经尝试过以下jquery但它不能正常工作

<script type="text/javascript" src="/site/js/jquery-1.10.2.min.js"></script>
<script>
$(function () {
if ($(".save-sale").text() == "0.00") {
$(".save-sale").hide();
$(".price-old").hide();
}
});
</script>

3 个答案:

答案 0 :(得分:2)

由于您拥有多个save-sale div,因此您可以使用.each()对所有div进行迭代,并检查其值,以隐藏save-saleprice-old

<script type="text/javascript" src="/site/js/jquery-1.10.2.min.js"></script>
<script>
$(function () {
  $(".save-sale").each(function(){
  if ($(this).text().trim() == "0.00") {
       $(this).hide();
       $(this).prev(".prod-price").find(".price-old").hide();
    }
  });
});
</script>

<强> DEMO

答案 1 :(得分:0)

DEMO FIDDLE

试试这个:

<script type="text/javascript" src="/site/js/jquery-1.10.2.min.js"></script>
<script>
  $(function () {
    if ($.trim($(".save-sale").text()) == "0.00") {
    $(".save-sale").hide();
    $(".price-old").hide();
  }
});
</script>

对于具有相同类名的多个Div,您可以尝试以下操作:

$(function () {
$(".save-sale").each(function (i) {
    if ($.trim($(this).text()) == "0.00") {
        $(this).closest(".save-sale").hide();
        $(this).closest(".price-old").hide();
    }
     else{
        $(this).closest(".save-sale").show();
        $(this).closest(".price-old").show();
    }
});
});

UPDATED FIDDLE

答案 2 :(得分:0)

您可以使用

  

$(&#34; .class或#id&#34;)。hide()

或类似但使用过的css风格

  

$(&#34; .class或#id&#34;)。css(&#39; display&#39;,&#39; none&#39;)

但是如果你想将它设置为具有类使用的所有元素,显然每个函数都是:

  

$(&#34;的.class&#34)。每一(功能()       {       $(本)的CSS(&#39;显示&#39;&#39;无&#39;)       });