-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathutil.js
55 lines (47 loc) · 1.26 KB
/
util.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
48
49
50
51
52
53
54
55
/*********************************辅助类util*************************************/
//辅助类 Util
var util = {};
util.type = function(obj) {
return Object.prototype.toString.call(obj).replace(/\[object\s|\]/g, '');
}
util.isArray = function(list) {
return util.type(list) === 'Array';
}
util.isString = function(list) {
return util.type(list) == 'String';
}
util.each = function(array, fn) {
for (var i = 0, len = array.length; i < len; i++) {
fn(array[i], i);
}
}
util.toArray = function(listLike) {
if (!listLike) {
return [];
}
var list = [];
for (var i = 0, len = listLike.length; i < len; i++) {
list.push(listLike[i]);
}
return list;
}
util.setAttr = function(node, key, value) {
switch (key) {
case 'style':
node.style.cssText = value;
break;
case 'value':
var tagName = node.tagName || '';
tagName = tagName.toLowerCase();
if (tagName === 'input' || tagName === 'textarea') {
node.value = value;
} else {
node.setAttribute(key, value);
}
break;
default:
node.setAttribute(key, value);
break;
}
}
module.exports = util;