Last active
August 29, 2015 14:13
-
-
Save z-------------/3b5dc59c9a1ebd5d7036 to your computer and use it in GitHub Desktop.
Check if an element matches a CSS selector string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HTMLElement.prototype.matchesSelector = function(selector) { | |
| var clonedElem = this.cloneNode(); | |
| var docFrag = new DocumentFragment(); | |
| docFrag.appendChild(clonedElem); | |
| return (docFrag.querySelectorAll(selector).length > 0); | |
| }; | |
| /* | |
| document.body.matchesSelector("body") // true | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment