如何隐藏“跨度"如果文本为"0",则为"0".使用jQuery吗? [英] How to hide "span" if it text is "0" using jQuery?

查看:89
本文介绍了如何隐藏“跨度"如果文本为"0",则为"0".使用jQuery吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找隐藏包含0的范围.我已经查看了其他代码,并尝试对其进行改编,但是我无法使其正常工作.我希望它仅在内容为"0"时才隐藏范围,但是在运行下面的代码时,它也将隐藏任何包含0(例如10)的数字,我不希望这样.

I'm looking to hide spans that contain a 0. I've looked at other code and I've tried to adapt it but I can't get it to work correctly. I want it to only hide the span when the contents is a "0", but when running the code below it also hides any number that contains 0, so 10 for example, which I don't want.

为了更清楚一点,跨度仅在其中的数字大于0时才显示(这是一个从0开始的计数器,因此无论如何不能小于0).

Just to make it a little clearer, the span should only display if the number inside it is greater than 0 (it's a counter that starts from 0 so can't be less than 0 anyway).

感谢您的帮助.

<div id="post-excerpts-likes">
    <a href="#" class="zilla-likes" id="zilla-likes-175519" title="Like this">
        <span class="zilla-likes-count">0</span>
    </a>
</div>

jQuery

$(".zilla-likes-count:contains('0')").hide();


请注意,页面上有多个跨度都属于同一类,我希望代码能影响所有这些跨度.


Please also note that there are going to multiple spans on the page all with the same class, I would like the code to affect them all.

推荐答案

您需要选择文本完全相同的元素,但 .filter() 是基于文本过滤所选元素的一个很好的功能.

You need to select element has exactly equal text but the :contains() isn't what you want. The .filter() is a good function to filtering selected element based on it text.

$(".zilla-likes-count").filter(function(){
  return $(this).text().trim() === "0";
}).hide();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="post-excerpts-likes">
    <a href="#" class="zilla-likes" id="zilla-likes-175519" title="Like this">
        <span class="zilla-likes-count">Text0Text</span>
        <span class="zilla-likes-count">0</span>
    </a>
</div>

这篇关于如何隐藏“跨度"如果文本为"0",则为"0".使用jQuery吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆