Change CSS in element that contains certain text

Asked

Viewed 123 times

1

I have the code to follow, where my intent is that where in the class .breadcrumb a appear the term "biblical", this class has its style changed only where the resolution is less than 320px, but so far it has not worked. What could be wrong, guys? Thanks in advance!

<script>
if ($(window).width() &lt; 320) {$(".breadcrumb a:contains('bíblicas')").css("background-color", "green");}
</script>
  • 1

    Let’s debug, this of some return?: $(".breadcrumb a:contains('bíblicas')")

  • @Daviddias, I used Hugo’s suggestion and it worked, man! Thanks for the kindness and the comment! God bless! =]

1 answer

1


I don’t see why use jQuery to do this if only with CSS you would solve.

But that way you get what you want using jQuery.

Option 1

$(window).resize(function(){     
    if ($('body').width() <= 320 ){
        $('a[href*="biblicas"]').css("background-color", "green");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

        <a href="https://code.jquery.com/jquery-3.2.1.slim.min.biblicas.js">https://code.jquery.com/jquery-3.2.1.slim.min.biblicas.js</a>


Option 2

$('body').append("<style type='text/css'>@media screen and (max-width: 320px) { a[href*='biblicas'] { background-color: green; } }</style>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="https://code.jquery.com/jquery-3.2.1.slim.min.biblicas.js">https://code.jquery.com/jquery-3.2.1.slim.min.biblicas.js</a>

  • Hugo, thanks for the answer! I used option 2, only with CSS I got what I wanted. It opened my mind to know this selector: https://www.w3schools.com/cssref/sel_attr_contain.asp God bless, man!

  • .append saving the rss day. Good luck with the project! About CSS search google for Attribute Css you will find some study material

  • Thank you, man! Here it is: http://einconformado.blogspot.com

Browser other questions tagged

You are not signed in. Login or sign up in order to post.