-
Notifications
You must be signed in to change notification settings - Fork 56
/
k0.6.html
338 lines (316 loc) · 8.85 KB
/
k0.6.html
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="box" style="color: red;">
<ul>
<li class="testLi">11</li>
<li class="testLi">12</li>
<li class="testLi">11</li>
<li class="testLi">12</li>
</ul>
</div>
<div id="pox">
<ul>
<li>pox</li>
<li>pox</li>
<li>pox</li><li>pox</li>
<li>pox</li>
</ul>
</div>
<div class="test">
<b>123</b>
</div>
<div class="test">
<span class="test-span"></span>
<b>321</b>
</div>
<b class="one-b"></b>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
(function(window, document) {
var w = window,
doc = document;
var Kodo = function(selector, context) {
return new Kodo.prototype.init(selector, context);
}
Kodo.prototype = {
constructor: Kodo,
length: 0,
splice: [].splice,
selector: '',
init: function(selector, context) { //dom选择的一些判断
if (!selector) {
return this;
}
if (typeof selector == 'object') {
this[0] = selector;
this.length = 1;
return this;
} else if (typeof selector == 'function') {
Kodo.ready(selector);
return;
}
var context = context || doc,
elm;
if (selector.charAt(0) == '#' && !selector.match('\\s')) {
this.selector = selector;
selector = selector.substring(1);
elm = doc.getElementById(selector);
this[0] = elm;
this.context = context;
this.length = 1;
return this;
} else {
elm = context.querySelectorAll(selector);
for (var i = 0; i < elm.length; i++) {
this[i] = elm[i];
}
this.selector = selector;
this.context = context;
this.length = elm.length;
return this;
}
},
css: function(attr, val) { //链式测试
///////////////////
/////////修改//////
///////////////////
for (var i = 0; i < this.length; i++) {
if(typeof attr == 'string') {
console.log(attr);
if (arguments.length == 1) {
return getComputedStyle(this[i], null)[attr];
}
this[i].style[attr] = val;
} else {
var _this = this[i];
f.each(attr,function(attr,val) {
_this.style.cssText += '' + attr + ':' + val + ';';
});
}
}
///////////////////
/////////修改//////
///////////////////
return this;
},
///////////////////
/////////新增//////
///////////////////
attr : function(attr, val) {
for (var i = 0; i < this.length; i++) {
if(typeof attr == 'string') {
if (arguments.length == 1) {
return this[i].getAttribute(attr);
}
this[i].setAttribute(attr,val);
} else {
var _this = this[i];
f.each(attr,function(attr,val) {
_this.setAttribute(attr,val);;
});
}
}
return this;
},
///////////////////
/////////新增//////
///////////////////
hasClass: function(cls) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
/////////////fixed///////////////
if (this[0].className.match(reg)) return true;
else return false;
/////////////fixed///////////////
return this;
},
addClass: function(cls) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
for (var i = 0; i < this.length; i++) {
if (!this[i].className.match(reg))
this[i].className += ' ' + cls;
}
return this;
},
removeClass: function(cls) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
for (var i = 0; i < this.length; i++) {
if (this[i].className.match(reg))
this[i].className = this[i].className.replace(' ' + cls, '');
}
return this;
},
find: function(selector) {
if (!selector) return;
var context = this.selector;
return new Kodo(context + ' ' + selector);
},
first: function() {
return new Kodo(this[0])
},
last: function() {
var num = this.length - 1;
return new Kodo(this[num]);
},
eq: function(num) {
var num = num < 0 ? (this.length - 1) : num;
console.log(num);
return new Kodo(this[num]);
},
get: function(num) {
var num = num < 0 ? (this.length - 1) : num;
console.log(num);
return this[num];
},
next: function() {
return sibling(this[0], "nextSibling");
},
prev: function() {
return sibling(this[0], "previousSibling");
},
parent: function() {
var parent = this[0].parentNode;
parent && parent.nodeType !== 11 ? parent : null;
var a = Kodo();
a[0] = parent;
a.selector = parent.tagName.toLocaleLowerCase();
a.length = 1;
return a;
},
parents: function() {
var a = Kodo(),
i = 0;
while ((this[0] = this[0]['parentNode']) && this[0].nodeType !== 9) {
if (this[0].nodeType === 1) {
a[i] = this[0];
i++;
}
}
a.length = i;
return a;
}
}
Kodo.prototype.init.prototype = Kodo.prototype;
//工具方法
Kodo.ready = function(fn) {
doc.addEventListener('DOMContentLoaded', function() {
fn && fn();
}, false);
doc.removeEventListener('DOMContentLoaded', fn, true);
}
Kodo.each = function(obj, callback) {
var len = obj.length,
constru = obj.constructor,
i = 0;
if (constru === window.f) {
for (; i < len; i++) {
var val = callback.call(obj[i], i, obj[i]);
if (val === false) break;
}
} else if (isArray(obj)) {
for (; i < len; i++) {
var val = callback.call(obj[i], i, obj[i]);
if (val === false) break;
}
} else {
for (i in obj) {
var val = callback.call(obj[i], i, obj[i]);
if (val === false) break;
}
}
}
Kodo.get = function(url, sucBack, complete) {
var options = {
url: url,
success: sucBack,
complete: complete
}
ajax(options);
}
Kodo.post = function(url, data, sucback, complete) {
var options = {
url: url,
type: "POST",
data: data,
success: sucback,
complete: complete
}
ajax(options);
}
function ajax(options) {
var defaultOptions = {
url: false, //ajax 请求地址
type: "GET",
data: false,
success: false, //数据成功返回后的回调方法
complete: false //ajax完成后的回调方法
};
for (i in defaultOptions) {
if (options[i] === undefined) {
options[i] = defaultOptions[i];
}
}
var xhr = new XMLHttpRequest();
var url = options.url;
var params = formatParams(options.data);
xhr.open(options.type, url);
xhr.onreadystatechange = onStateChange;
if (options.type === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
xhr.send(params ? params : null);
function onStateChange() {
if (xhr.readyState == 4) {
var result,
status = xhr.status;
if ((status >= 200 && status < 300) || status == 304) {
result = xhr.responseText;
ajaxSuccess(result, xhr)
} else {
console.log("ERR", xhr.status);
}
}
}
function ajaxSuccess(data, xhr) {
var status = 'success';
options.success && options.success(data, options, status, xhr)
ajaxComplete(status)
}
function ajaxComplete(status) {
options.complete && options.complete(status);
}
function formatParams(data) {
var arr = [];
for (var name in data) {
arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
}
arr.push(("v=" + Math.random()).replace("."));
return arr.join("&");
}
}
function sibling(cur, dir) {
while ((cur = cur[dir]) && cur.nodeType !== 1) {}
return cur;
}
function isArray(obj) {
return Array.isArray(obj);
}
window.f = Kodo;
})(window, document);
var bs = f('b');
console.log(bs);
f.each(bs,function(index,item) {
console.log(f(this));
})
console.log('//////////////////')
$.each(bs,function(index,item) {
console.log($(this));
})
var obj = {"width":'200px',"height":"200px","background":"green"};
</script>
</body>
</html>