Con JavaScript possiamo verificare se un elemento corrisponde ad un selettore CSS.
La soluzione รจ la seguente:
'use strict';
const is = (el, selector) => {
return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector);
};
Esempio d'uso:
let el = document.querySelector('#test');
is(el, '#test'); // true