javascript - Determine what CSS selectors are been applied to an element -
is there way determine if selector been applied given element?
i know it´s possible iterate on css selectors, , test if each 1 applicably or not. i´m not sure if way firebug , other inspector it.
edit: need way dynamically, js.
you can check if element instance matched selector using document.queryselectorall
, array.prototype.indexof
:
function elementmatchesselector(element, selector) { return array.prototype.indexof.call(document.queryselectorall(selector), element) > -1; }
of course works modern browsers support aforementioned methods.
alternatively can use element.matches
:
function elementmatchesselector(element, selector) { var fn; if (!element) { return false; } fn = element.matches || element.mozmatchesselector || element.msmatchesselector || element.webkitmatchesselector; if (fn) { return fn.call(element, selector); } return false; }
Comments
Post a Comment