-
Notifications
You must be signed in to change notification settings - Fork 9
/
vquery.js
47 lines (41 loc) · 1.44 KB
/
vquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* vQuery library
*
* Copyright 2013 Trey Hunner. Released under MIT license.
*
* Exposes vQuery objects and extends Element and NodeList
*
* vQuery function and vQuery methods (except get) return NodeList of matches
* vQuery: Selector and optional node -> NodeList of matches
* vQuery.byTag: Tag name and optional node -> NodeList of matches
* vQuery.byClass: Class name and optional node -> NodeList of matches
* vQuery.get: Selector and optional node -> first matching node
*
* Extensions of the DOM:
* - Adds "on" method to Element
* - Adds "forEach" method to NodeList
*
*/
(function() {
var nodeOrDocument = function (arg) {
return function(selector, node) {
if (!node) node = document;
return node[arg].call(node, selector);
};
};
var toArray = function (func) {
return function () {
var listLikeObj = func.apply(window, arguments);
return Array.prototype.slice.call(listLikeObj, 0);
};
};
NodeList.prototype.forEach = Array.prototype.forEach;
Element.prototype.on = Element.prototype.addEventListener;
var $ = nodeOrDocument('querySelectorAll');
$.byTag = nodeOrDocument('getElementsByTagName');
$.byClass = nodeOrDocument('getElementsByClassName');
$.get = nodeOrDocument('querySelector');
$.getByClass = function (s) { return $.byClass(s)[0]; };
$.getByTag = function (s) { return $.byTag(s)[0]; };
$.getById = nodeOrDocument('getElementById');
window.vQuery = $;
}());