-
Notifications
You must be signed in to change notification settings - Fork 56
/
k0.1.html
108 lines (98 loc) · 2.84 KB
/
k0.1.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
<!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">312</li>
<li class="testLi">11</li>
<li class="testLi">12</li>
<li class="testLi">12</li>
<li class="testLi">312</li>
</ul>
</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) {
return new Kodo.prototype.init(selector);
}
Kodo.prototype = {
constructor : Kodo,
length : 0,
splice: [].splice,
selector : '',
init : function(selector) {//dom选择的一些判断
if(!selector) { return this; }
var selector = selector.trim(),
elm;
if (selector.charAt(0) == '#' && !selector.match('\\s')) {
selector = selector.substring(1);
this.selector = selector;
elm = document.getElementById(selector);
this[0] = elm;
this.length = 1;
return this;
} else {
elm = document.querySelectorAll(selector);
for (var i = 0; i < elm.length; i++) {
this[i] = elm[i];
}
this.selector = selector;
this.length = elm.length;
return this;
}
},
css : function(attr,val) {//链式测试
console.log(this.length);
for(var i = 0;i < this.length; i++) {
if(arguments.length == 1) {
return getComputedStyle(this[i],null)[attr];
}
this[i].style[attr] = val;
}
return this;
},
hasClass : function(cls) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
for (var i = 0; i < this.length; i++) {
if (this[i].className.match(reg)) return true
return false;
}
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;
}
}
Kodo.prototype.init.prototype = Kodo.prototype;
Kodo.ajax = function() { //直接挂载方法 可k.ajax调用
console.log(this);
}
window.f = Kodo;
})(window,document);
console.log(f('li'))
</script>
</body>
</html>