Given a CSS selector, returns true or false depending on wether the selector is defined in any included or embedded stylesheet.
131bytes
function( | |
a, // The CSS selector to search for | |
b,c,d,e // Placeholders | |
){ | |
for(c in b=document.styleSheets) // Iterate over every stylesheet | |
for(d in e=b[c].rules||b[c].cssRules) // then iterate over every rule | |
if(e[d].selectorText==a) // to see if it matches the input | |
return!0; // If so, return true! | |
return!b // No match :/ | |
} |
function(a,b,c,d,e){for(c in b=document.styleSheets)for(d in e=b[c].rules||b[c].cssRules)if(e[d].selectorText==a)return!0;return!b} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2013 Johan Hillerström <https://github.com/hillerstorm> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "cssSelectorDefinedOrNot", | |
"description": "Checks to see if a given CSS selector is defined in any included stylesheet.", | |
"keywords": [ | |
"css", | |
"selector" | |
] | |
} |
<!DOCTYPE html> | |
<title>CSS-selector defined or not?</title> | |
<style> | |
.this-exists{ | |
} | |
</style> | |
<div>Expected value: <b>true</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var exists = function(a,b,c,d,e){for(c in b=document.styleSheets)for(d in e=b[c].rules||b[c].cssRules)if(e[d].selectorText==a)return!0;return!b} | |
document.getElementById( "ret" ).innerHTML = exists('.this-exists') | |
</script> |
Even for..in loops can be used for definitions (save 5 chars):