HTMLElement.prototype.find = function(selector, context) {
if (!context) {
context = document.body;
}
//так как лень было искать эмулятор некробраузеров,
//предположим что такой перед нами
/*
if (this.querySelectorAll) {
return context.querySelectorAll(selector);
}
*/
//if (!HTMLElement.prototype.getElementsByClassName) {
if (true) {
HTMLElement.prototype.getElementsByClassName = getElementsByClassName;
}
var checks = [
{reg: /^\.(\w+-?\w+)/, func: context.getElementsByClassName},
{reg: /^#(\w+-?\w+)/, func: document.getElementById.bind(document)},
{reg: /^(\w+)$/, func: context.getElementsByTagName.bind(context)}
];
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);
if (!(result instanceof HTMLElement)) {
result = Array.prototype.slice.call(result);
}
array = array.concat(result);
break;
}
if (array.length > 0) {
return array;
} else {
return null
}
};
function getElementsByClassName(selector, context) {
var elems = context.getElementsByTagName("*");
//console.log(elems);
var result = [];
for (var i = 0; i < elems.length; i++) {
//console.log(elems[i].className, selector);
if (elems[i].className == selector) {
result.push(elems[i]);
}
}
return result;
}
var div = document.createElement("div");
document.body.appendChild(div);
var test...
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.
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!
Console
Debug your Fiddle with a minimal built-in JavaScript console.