-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphi.js
210 lines (190 loc) · 5.32 KB
/
phi.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
(function (window) {
var phis = window.phis = window.phis || {} , e = null;
phis = {
init : function () {
try {
console.log('OK');
return true;
} catch(e) {
console.error('failed');
}
},
addloadEvent : function(func)
{
var oldonLoad = window.onload;
if( typeof window.onload != 'function' )
{
window.onload = func;
} else
{
window.onload = function (){
oldonLoad();
func();
}
}
},
insertAfter : function(newElement , targetElement)
{
var parent = targetElement.parentNode;
if( parent.lastChild == targetElement )
{
parent.appendChild( newElement );
} else
{
parent.insertBefore( newElement , targetElement.nextSibling );
}
},
getHttpObject : function()
{
if ( typeof XMLHttpRequest == 'undefined')
XMLHttpRequest = function(){
try
{
return new ActiveXObject( "Msxml2.XMLHTTP.6.0" );
}
catch ( e ){}
try
{
return new ActiveXObject( "Msxml2.XMLHTTP.3.0" );
}
catch (e){}
try
{
return new ActiveXObject( "Msxml2.XMLHTTP" );
}
catch (e){}
return false;
}
return new XMLHttpRequest();
},
//Hijax : 渐近增强地使用Ajax。使用JavaScript去拦截默认动作而不是一开始就使用JavaScript。
getNextElement : function( node )
{
if( node.nodeType == 1 ) {
return node;
}
if( node.nextSibling ) {
return getNextElement( node.nextSibling );
}
return null;
},
stripeTables : function( colors ) {
if( !document.getElementsByTagName) return false;
var tables = document.getElementsByTagName( "table" );
var tablesLength = tables.length;
for(var i = 0; i <= tablesLength; i++) {
var odd = false;
var rows = document.getElementsByTagName( "tr" );
var rowsLength = rows.length;
for(var j = 0; j <= rowsLength; j++) {
if( odd == true ) {
rows[j].style.backgroundColor = colors;
odd = false;
} else
{
odd = true;
}
}
}
},
heightLightRows : function() {
if( !document.getElementsByTagName ) return false;
var rows = document.getElementsByTagName( "tr" );
var rowsLength = rows.length;
for(var i = 0; i <= rowsLength; i++) {
rows[i].onmouseover = function () {
this.style.fontWeight = 'bold';
}
rows.onmouseout = function() {
this.style.fontWeight = 'normal';
}
}
},
addClass :function( element , value ) {
if( !element.className )
{
element.className = value;
} else
{
newClassName = element.className;
newClassName += " ";
newClassName += value;
element.className = newClassName;
}
},
styleElementSibling : function( tag , theClass ) {
if ( !document.getElementsByTagName) return false;
var elems = document.getElementsByTagName( tag );
var elem;
var elemsLength = elems.length;
for( var i=0; i<= elemsLength; i++ ) {
elem = getNextElement( elems[i].nextSibling );
addClass( elem , theClass );
}
},
moveElement : function( elementID , finaX , finaY , interval ) {
if( !document.getElementById ) return false;
if( !document.getElementById( elementID ) ) return false;
var elem = document.getElementById( elementID );
if ( elem.movement )
{
clearTimeout( elem.movement );
}
if ( !elem.style.top )
{
elem.style.top = 0 + "px";
}
if ( !elem.style.left )
{
elem.style.left = 0 +"px";
}
var posX = parseInt( elem.style.left );
var posY = parseInt( elem.style.top );
var dist = 0;
if ( posX == finaX && posY == finaY)
{
return true;
}
if ( posX < finaX )
{
dist = Math.ceil( (finaX -posX) /10 );
posX = posX + dist;
}
if ( posX > finaX )
{
dist = Math.ceil( (finaX -posX) /10 );
posX = posX - dist;
}
if ( posY < finaY )
{
dist = Math.ceil( ( finaY - posY )/ 10 );
posY = posY + dist;
}
if ( posY > finaY )
{
dist = Math.ceil( ( finaY - posY )/ 10 );
posY = posY - dist;
}
elem.style.left = posX + "px";
elem.style.top = posY + "px";
var repeat = "moveElement('" + elementID + "' , "+finaX+" , "+finxY+" , "+interval+")";
elem.movement = setTimeout( repeat , interval );
},
//统计出现次数最多的字符
counterStr : function( str ) {
var maxLenght = 0;//命名一个变量放置字母出现的最高次数并初始化为0
var result = ''; //命名一个变量放置结果输入
while( str != '' ) { //循环迭代开始,并判断字符串是否为空
oldStr = str;//将原始字符串保存
getStr = str.substr( 0 , 1 );//取得字符串的第一个字符
eval( "str = str.replace(/"+getStr+"/g , '' )" );//剔除第一个元素
if( oldStr.length - str.length > maxLength ) {//判断原始的字符串长度减去替代后字符串长度是否大于之前出现的最大的字符串长度
maxLength = oldStr.length - str.length;//两字符串长度相减得到最大的字符串长度
result = getStr + " = " + maxLength;//返回最大的字符串结果(字母、出现次数)
}
}
return result;
}
}
return phis;
}(window));