Change css depending on span value

时间:2016-04-04 17:18:37

标签: jquery html css

I have a span and a div. I need to make it where if the value of span is 123, change the css of the div to display:none;

I'm trying to do it with jQuery, but I cannot see where I am going wrong. Here is my code:

(by the way, I'm using jQuery no conflict)

      <script>
        jQuery(function ($) {
jQuery('span.specialspan').filter(function(){
    return $.trim($(this).text()) == '123'
}).css('display', 'none');
});
</script>

<span class="specialspan">123</span>

<div id="specialdiv">Foo</div>

Thanks for your help!

1 个答案:

答案 0 :(得分:1)

You can use each() to check each span and next() to hide specialdiv after span with 123

$('span').each(function() {
  if ($.trim($(this).text()) == '123') {
    $(this).next('.specialdiv').css('display', 'none');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="specialspan"> 123 </span>
<div class="specialdiv">Foo</div>
<br>
<span class="specialspan">lorem</span>
<div class="specialdiv">Lorem</div>