forked from taoyingsong/FEND_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.compatible.js
304 lines (253 loc) · 8.67 KB
/
base.compatible.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
实现浏览器兼容版的element.children
*/
function getChildElements(elementObj) {
// if(elementObj.children) {
// return elementObj.children;
// }
// else {
var childs = [];
var childElement;
for (var i = 0; i < elementObj.childNodes.length; i++) {
childElement = elementObj.childNodes[i];
if (childElement.nodeType == 1) {
childs.push(childElement);
}
}
return childs;
// }
}
/*
实现浏览器兼容版的element.dataset
*/
function dataset(element) {
//判断当前浏览器是否原生支持dataset属性
if (element.dataset) {
return element.dataset;
}
//data-*自定义属性以“-”拆分后得到的数组
//返回的属性对象
//得到的新的属性名
var arr = [],
result = {},
attrName;
//获取element元素的所有属性集合
var attr = element.attributes;
for (var i = 0; i < attr.length; i++) {
//获取data-*的自定义属性
if (attr[i].name.slice(0, 5) == "data-") {
//data-*自定义属性以“-”拆分后得到的数组
arr = attr[i].name.split("-");
//从第二个单词开始
for (var j = 1; j < arr.length; j++) {
if (j > 1) {
//第二个单词开始首字母大写
attrName += arr[j].slice(0, 1).toUpperCase() + arr[j].slice(1);
} else {
//第一个单词还是保持小写
attrName = arr[j];
}
}
//将属性名和值存入返回对象中 }
result[attrName] = attr[i].value;
}
}
return result;
}
/*
bind 的低版本兼容
*/
if (!Function.prototype.bind) {
Function.prototype.bind = function(obj) {
var self = this;
var selfArg = Array.prototype.slice.call(arguments, 1);
var bridge = function() {};
bridge.prototype = self.prototype;
var _self = function() {
var newArg = selfArg.concat(Array.prototype.slice.call(arguments));
return self.apply(this instanceof bridge ? this : obj || {}, newArg)
};
_self.prototype = new bridge();
return _self;
}
}
/*
实现浏览器兼容版的window.getComputedStyle
*/
function computedStyle(element) {
return window.getComputedStyle ? window.getComputedStyle(element) : element.currentStyle;
}
/*
element.innerText兼容
*/
if (!('innerText' in document.body)) {
HTMLElement.prototype._defineGetter_("innerText", function() {
return this.textContent;
});
HTMLElement.prototype._defineSetter_("innerText", function(s) {
return this.textContent = s;
});
}
/*
getElementsByClass兼容
demo:
<div id="example">
<p id="p1" class="aaa bbb"/>
<p id="p2" class="aaa ccc"/>
<p id="p3" class="bbb ccc"/>
</div>
console.log(getElementsByClassName(example, "aaa"));
console.log(getElementsByClassName(example, "bbb ccc"));
console.log(getElementsByClassName(example, "ccc bbb"));
*/
function getElementsByClassName(element, names) {
if (element.getElementsByClassName) {
return element.getElementsByClassName(names);
} else {
var elements = element.getElementsByTagName('*');
var result = [];
var element,
classNameStr,
flag;
names = names.split(' ');
for (var i = 0; element = elements[i]; i++) {
classNameStr = ' ' + element.className + ' ';
flag = true;
for (var j = 0, name; name = names[j]; j++) {
if (classNameStr.indexOf(' ' + name + '') == -1) {
flag = false;
break;
}
}
if (flag) {
result.push(element);
}
}
return result;
}
}
/*
addEvent delEvent 浏览器兼容
*/
var addEvent = document.addEventListener ?
function(elem, type, listener, useCapture) {
elem.addEventListener(type, listener, useCapture);
} :
function(elem, type, listener, useCapture) {
elem.attachEvent('on' + type, listener);
};
var delEvent = document.removeEventListener ?
function(elem, type, listener, useCapture) {
elem.removeEventListener(type, listener, useCapture);
} :
function(elem, type, listener, useCapture) {
elem.detachEvent('on' + type, listener);
}
/*
Object.create()兼容
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
*/
if (typeof Object.create != 'function') {
// Production steps of ECMA-262, Edition 5, 15.2.3.5
// Reference: http://es5.github.io/#x15.2.3.5
Object.create = (function() {
// To save on memory, use a shared constructor
function Temp() {}
// make a safe reference to Object.prototype.hasOwnProperty
var hasOwn = Object.prototype.hasOwnProperty;
return function(O) {
// 1. If Type(O) is not Object or Null throw a TypeError exception.
if (typeof O != 'object') {
throw TypeError('Object prototype may only be an Object or null');
}
// 2. Let obj be the result of creating a new object as if by the
// expression new Object() where Object is the standard built-in
// constructor with that name
// 3. Set the [[Prototype]] internal property of obj to O.
Temp.prototype = O;
var obj = new Temp();
Temp.prototype = null; // Let's not keep a stray reference to O...
// 4. If the argument Properties is present and not undefined, add
// own properties to obj as if by calling the standard built-in
// function Object.defineProperties with arguments obj and
// Properties.
if (arguments.length > 1) {
// Object.defineProperties does ToObject on its first argument.
var Properties = Object(arguments[1]);
for (var prop in Properties) {
if (hasOwn.call(Properties, prop)) {
obj[prop] = Properties[prop];
}
}
}
// 5. Return obj
return obj;
};
})();
}
//自己的Object.create兼容方案
if (typeof Object.create != 'function') {
Object.create = (function() {
function F() {};
return function(proto) {
if (typeof proto != 'object') {
throw TypeError('原型只能是对象或者null');
}
F.prototype = proto;
var obj = new F();
F.prototype = null;
return obj;
};
})();
}
/*
forEach的IE兼容方案
参考:
http://blog.csdn.net/oscar999/article/details/8671546
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
*/
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(' this is null or not defined');
}
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as the this value and
// argument list containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}