It is possible, but all around!
Check out this demo: http://jsfiddle.net/BE47z/1/
The logic is as follows:
- List all classes with the definition
:before or :after in CSS - this is done by moving the document.styleSheets object (you can change the code only to catch :before or :after , etc.). as needed) - Once you get the list of classes, delete the part after: its name in CSS, for example:
.a:before becomes .a - Now you can get all the elements corresponding to these classes
code
HTML
<div class="element classWithoutBefore">No :before</div> <div class="element classWithBefore">Has :before</div> <div class="element class2WithBefore">Has :before</div> <div class="element class2WithoutBefore">No :before</div>
CSS
.classWithoutBefore { } .class2WithoutBefore { } .classWithBefore:before { } .class2WithBefore:before { } .a:before, .b:before { }
Js
var sheets = document.styleSheets; var pseudoClasses = [], i = 0, j = 0; for (i=0; i<sheets.length; i++) { try { var rules = sheets[i].cssRules; for (j=0; j<rules.length; j++) { if(rules[j].selectorText.indexOf(':before') != -1 || rules[j].selectorText.indexOf(':after') != -1) { pseudoClasses.push(rules[j].selectorText); } } } catch(ex) {
source share