【问题标题】:javascript / jQuery hide if html equals 0如果 html 等于 0,则 javascript / jQuery 隐藏
【发布时间】:2016-05-09 08:59:51
【问题描述】:

我已经使用 Wordpress 建立了这个网站www.piano-warehouse.co.uk/category/upright-pianos/,并使用了一个插件来计算如果对产品应用折扣可以节省的金额。我想修改设置,以便如果没有应用折扣并且没有保存金额来隐藏您保存的部分。我认为 javascript 或 jQuery 将是门票,但是我对这些语言有非常基本的了解,并且非常感谢构建此查询的任何帮助。

到目前为止,我已经尝试将它添加到我的函数文件中,但没有任何乐趣

function you_save_hide () {
    if ($("span.yousave_list_price").text() == "0") {
        $("span.yousave_list").hide();
    }
}

【问题讨论】:

  • 包括 html 以及
  • 我猜"span.yousave_list_price" 也应该被隐藏?!
  • 你也许应该隐藏一个 div,而不是 1+ span(s)。请出示您的 html
  • “HTML 等于 0”是怎么回事?

标签: javascript php jquery html wordpress


【解决方案1】:

从链接中我可以看到$("span.yousave_list_price") 是否包含另一个 DOM 元素

<span class="yousave_list_price">
<span class="amount">Some Value</span>
</span>

所以如果你想隐藏这个span.yousave_list_price,你可能不得不这样做

var _getAmountList = $('.amount');
_getAmountList.each(function(){
// equalizing with £0 just for demo
// You can use regex with fine tune it.
if($(this).text().trim() ==="£0"){ 
  $(this).parent().hide()
}
})

JSFIDDLE

【讨论】:

    【解决方案2】:

    调用此函数时必须确保页面准备就绪

    $(document).ready(function(){
        you_save_hide();
    });
    

    【讨论】:

      【解决方案3】:

      我检查了您的网站,span.yousave_list_price 是 span.yousave_list 的下一个元素,我确定您不希望隐藏所有 span.yousave_list 的 0。但只有那些具有 span.yousave_list_price 的元素。所以我们将选择所有 span.yousave_list_price -> 检查文本是否为 0 -> 然后我们选择它的父级 -> 在父级中隐藏 span.yousave_list。

      function you_save_hide () {
          $("span.yousave_list_price").each(function(){
              if ($(this).text().trim() == "0"){
                  $(this).parent().closest("span.yousave_list").hide(); //hide the nearest .yousave_list when the yousave_list_price equals 0
              }
          });
      }
      

      如果您在页面加载时执行此函数,请务必在 $(document).ready(function(){...} 中调用它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多