style:eq(2)").hasClass('clas...">

Jquery - How to check if class name exists

I want to check the style tag if the class name exists.

if ($("head > style:eq(2)").hasClass('className')) 
{
  alert('yes');
}
+3
source share
2 answers

You can access the object document.styleSheets:

see all the way in this answer

get percentage value of CSS rule in jQuery

<script>
    var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
    for (var i=0; rules.length; i++) {
        var rule = rules[i];
        if (rule.selectorText.toLowerCase() == ".classname") {
            alert('found!!');
        }
    }
</script>
+6
source

Another version, the same, only the difference is that sometimes for me selectorText is not defined

var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
for (var i in rules) 
{
    if (typeof rules[i]['selectorText'] != 'undefined' && rules[i]['selectorText'].indexOf("fbconnect_button") >= 0) 
    {
        alert('found!!');
    }
}
0
source

Source: https://habr.com/ru/post/1760968/


All Articles