function find(selector, context) {
if (!context) {
context = document.body;
}
//так как лень было искать эмулятор некробраузеров,
//предположим что такой перед нами
/*
if (context.querySelectorAll) {
return context.querySelectorAll(selector);
}
*/
//if (!HTMLElement.prototype.getElementsByClassName) {
if (true) {
HTMLElement.prototype.getElementsByClassName = getElementsByClassName;
}
var checks = [
{reg: /^\.(\w+-?\w+)/, func: pipe(context.getElementsByClassName, collectionToArray)},
{reg: /^#(\w+-?\w+)/, func: getElemByIdWithContext},
{reg: /^(\w+)$/, func: pipe(context.getElementsByTagName.bind(context), collectionToArray)}
];
selector = selector.trim();
var array = [];
for (var i = 0; i < checks.length; i++) {
var match = selector.match(checks[i].reg);
//console.log(match);
if (!match) {
continue;
}
//console.log(checks[i].func);
var result = checks[i].func(match[1], context);
array = array.concat(result);
break;
}
if (array.length > 0) {
return array;
} else {
return null
}
}
function pipe(func1, func2) {
return function() {
var result = func1.apply(null, collectionToArray(arguments));
return func2(result);
}
}
function collectionToArray(collection) {
return Array.prototype.slice.call(collection);
}
function getElementsByClassName(selector, context) {
//console.log(elems);
var result = [];
var elements = context.children;
//console.log(elements);
if (!elements) {
return null;
}
var regex = new RegExp("(^|\\s+)" + selector + "($|\\s+)");
for...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.