javascript 基础类,实现命名空间、基础比较、事件操作、ajax、dom元素操作等。
注释及文档还未整理。
1 /** 2 * @说明 基础javascript 可以与jquery同时使用 基础javascript 3 * @author xxd 4 * @date 20140729 5 */ 6 /** 7 * 以 _ 开头函数为私有函数 8 */ 9 10 11 /*命名空间函数 避免冲突*/ 12 var GLOBAL={}; 13 GLOBAL.namespace = function(str){ 14 var arr = str.split("."),o=GLOBAL; 15 for(i=(arr[0]=="GLOBAL")?1:0;i<arr.length;i++){ 16 o[arr[i]] = o[arr[i]]||{}; 17 o =o[arr[i]]; 18 } 19 } 20 /*DOM对象操作*/ 21 GLOBAL.namespace("Dom"); 22 /*取得DOM节点,通过id 或者node 根据node 类型*/ 23 GLOBAL.Dom.getNextNode=function(node){ 24 node = (typeof node=="string") ? document.getElementById(node):node; 25 var nextNode = node.nextSibling; 26 if(!nextNode) return null; 27 if(!document.all) 28 { 29 while(true) 30 { 31 if(nextNode.nodeType==1) 32 { 33 break; 34 } 35 else 36 { 37 if(nextNode.nextSibling) 38 { 39 nextNode = nextNode.nextSibling; 40 } 41 else 42 { 43 break; 44 } 45 } 46 } 47 } 48 return nextNode; 49 } 50 /*通过类名获得Dom节点*/ 51 GLOBAL.Dom.getElementByClassName=function(str,root,tag){ 52 if(root) 53 { 54 root=typeof root=="string"?document.getElementById(root):root; 55 }else 56 { 57 root = document.body; 58 } 59 tag =tag||"*"; 60 var els = root.getElementsByTagName(tag),arr=[]; 61 for(var i=0,n=els.lenght;i<n;i++) 62 { 63 for(var j=0,k=els[i].classname.split(" "),l=k.length;j<l;j++) 64 { 65 if(k[j]==str) 66 { 67 arr.push(els[i]); 68 break; 69 } 70 } 71 } 72 return arr; 73 } 74 /*更具ID得到DOM*/ 75 GLOBAL.Dom.get=function(node){ 76 node = (typeof node=="string")?document.getElementById(node):node; 77 return node; 78 } 79 /*设置DOM CSS样式*/ 80 81 82 GLOBAL.Dom.setClass =function(node,str){ 83 if(!new RegExp("(^|\\s+)"+str).test(node.className)) 84 { 85 node.className = str; 86 } 87 } 88 89 GLOBAL.Dom.setStyle = function(p_elem, p_styles) { 90 var s; 91 for (s in p_styles) { 92 p_elem.style[s] = p_styles[s] 93 } 94 } 95 96 97 GLOBAL.Dom.setAttribute = function(p_elem, p_styles) { 98 var s; 99 for (s in p_styles) { 100 p_elem.setAttribute(s,p_styles[s]); 101 } 102 } 103 104 GLOBAL.Dom.addClass = function(element, classToAdd) { 105 var currentClassValue = element.className; 106 if (currentClassValue.indexOf(classToAdd) == -1) { 107 if ((currentClassValue == null) || (currentClassValue === "")) { 108 element.className = classToAdd 109 } else { 110 element.className += " " + classToAdd 111 } 112 } 113 } 114 GLOBAL.Dom.removeClass = function(element, classToRemove) { 115 var currentClassValue = element.className; 116 if (currentClassValue == classToRemove) { 117 element.className = ""; 118 return 119 } 120 var classValues = currentClassValue.split(" "); 121 var filteredList = []; 122 for (var i = 0; i < classValues.length; i++) { 123 if (classToRemove != classValues[i]) { 124 filteredList.push(classValues[i]) 125 } 126 } 127 element.className = filteredList.join(" ") 128 } 129 GLOBAL.Dom.addStyleNode = function(strCss) 130 { 131 var styleNode = document.createElement("style"); 132 styleNode.type = "text/CSS"; 133 if(styleNode.styleSheet) 134 { 135 styleNode.styleSheet.cssText = strCss; 136 } 137 else 138 { 139 styleNode.innerHTML = strCss; 140 } 141 document.getElementsByTagName("head")[0].appendChild(styleNode); 142 } 143 144 145 /*操作类*/ 146 GLOBAL.namespace("TOOL"); 147 /*设置节点透明度*/ 148 GLOBAL.TOOL.setOpactity=function(node,level){ 149 node = (typeof node=="string")?document.getElementById(node):node; 150 if( document.all) 151 { 152 node.style.filter = ‘alpha(opacity=‘+level+‘)‘; 153 }else 154 { 155 node.style.opacity=level/100; 156 } 157 } 158 159 GLOBAL.TOOL.trim=function(ostr) 160 { 161 return ostr.replace(/^\s+|\s+$/g,""); 162 } 163 GLOBAL.TOOL.isNumber=function(s) 164 { 165 return !isNaN(s); 166 } 167 GLOBAL.TOOL.isString=function(s) 168 { 169 return typeof s==="string"; 170 } 171 GLOBAL.TOOL.isBoolean=function(s) 172 { 173 return typeof s==="boolean"; 174 } 175 GLOBAL.TOOL.isFunction=function(s) 176 { 177 return typeof s==="function"; 178 } 179 GLOBAL.TOOL.isNull=function(s) 180 { 181 return s===null; 182 } 183 184 GLOBAL.TOOL.isUndefined=function(s) 185 { 186 return typeof s==="undefined"; 187 } 188 189 GLOBAL.TOOL.isEmpty=function(s) 190 { 191 return /^\s*$/.test(s); 192 } 193 194 GLOBAL.TOOL.isArray=function(s) 195 { 196 return s instanceof Array; 197 } 198 199 GLOBAL.TOOL.getNodeSize = function(w) { 200 var w = w; 201 return { 202 w: w.clientWidth, 203 h: w.clientHeight 204 } 205 } 206 GLOBAL.TOOL.cutString=function(str, len) { 207 if(str.length*2 <= len) { 208 return str; 209 } 210 var strlen = 0; 211 var s = ""; 212 for(var i = 0;i < str.length; i++) { 213 s = s + str.charAt(i); 214 if (str.charCodeAt(i) > 128) { 215 strlen = strlen + 2; 216 if(strlen >= len){ 217 return s.substring(0,s.length-1) + "..."; 218 } 219 } else { 220 strlen = strlen + 1; 221 if(strlen >= len){ 222 return s.substring(0,s.length-2) + "..."; 223 } 224 } 225 } 226 return s; 227 } 228 /**时间间隔函数interval :D表示查询精确到天数的之差 229 230 interval :H表示查询精确到小时之差 231 232 interval :M表示查询精确到分钟之差 233 234 interval :S表示查询精确到秒之差 235 236 interval :T表示查询精确到毫秒之差**/ 237 GLOBAL.TOOL.dateDiff= 238 function(interval, date1, date2) 239 { 240 var objInterval = {‘D‘:1000 * 60 * 60 * 24,‘H‘:1000 * 60 * 60,‘M‘:1000 * 60,‘S‘:1000,‘T‘:1}; 241 interval = interval.toUpperCase(); 242 var dt1 = date1; 243 var dt2 = date2; 244 try 245 { 246 //alert(dt2.getTime() - dt1.getTime()); 247 //alert(eval_r(‘objInterval.‘+interval)); 248 //alert((dt2.getTime() - dt1.getTime()) / eval_r(‘objInterval.‘+interval)); 249 return Math.round((dt2.getTime() - dt1.getTime()) / eval(‘objInterval.‘+interval)); 250 } 251 catch (e) 252 { 253 return e.message; 254 } 255 } 256 257 258 /*继承*/ 259 GLOBAL.TOOL.extend=function(subClass,superClass) 260 { 261 var F=function(){}; 262 F.prototype=superClass.prototype; 263 subClass.prototype = new F(); 264 subClass.prototype.constructor = subClass; 265 subClass.superclass = superClass.prototype; 266 if(superclass.prototype.constructor == Object.prototype.constructor) 267 { 268 superclass.prototype.constructor = superClass; 269 } 270 } 271 /* 272 * @parameters 为一个对象 273 * 返回一个带值的url字符串 274 */ 275 GLOBAL.TOOL.creatGETParam=function(url,parameters) 276 { 277 var rtnStr= null; 278 var tmpStr=‘‘,str; 279 var i = 0; 280 rtnStr = url+"?"; 281 for (var key in parameters) { 282 str = escape(key) +"="+escape(parameters[key]); 283 if(i==0) 284 { 285 tmpStr+=str; 286 i++; 287 } 288 else{ 289 tmpStr+=‘&‘+str; 290 } 291 } 292 rtnStr+=tmpStr; 293 return rtnStr; 294 } 295 296 GLOBAL.TOOL.cratePOSTParam=function(parameters) 297 { 298 var rtnStr =‘‘; 299 var i = 0; 300 var tmpStr=‘‘,str; 301 for(var key in parameters) 302 { 303 str = escape(key) +"="+escape(parameters[key]); 304 if(i==0) 305 { 306 tmpStr+=str; 307 i++; 308 } 309 else 310 { 311 tmpStr+=‘&‘+str; 312 } 313 } 314 return tmpStr; 315 } 316 317 //查找字符 318 GLOBAL.TOOL.isHasStr =function(s,sfind) 319 { 320 if(typeof s==="string" && typeof sfind=="string") 321 { 322 if(s.search(sfind)!=-1) 323 { 324 return true; 325 } 326 } 327 return false; 328 } 329 330 //获取元素或窗口的scrollTop 和 scrollleft IE 8+ firefox 331 GLOBAL.TOOL.getScrollOffsets=function(w){ 332 var w = w || window; 333 if (w.pageXoffset != null) { 334 return { x: w.pageXoffset, y: pageYoffset}; 335 } 336 var d = w.document; 337 return {x:d.documentElement.scrollLeft?d.documentElement.scrollLeft:d.body.scrollLeft,y:d.documentElement.scrollTop?d.documentElement.scrollTop:d.body.scrollTop}; 338 } 339 340 //获取浏览器窗口大小 或者元素 341 GLOBAL.TOOL.getViewPortSize=function(w){ 342 var w = w || window; 343 if (w.innerWidth != null) 344 return { w: w.innerWidth, h: w.innerHeight }; 345 var d = w.document; 346 if (document.compatMode == "CSS1Compat") 347 return { w: d.documentElement.clientWidth, h: d.documentElement.clientHeight }; 348 return { w: d.body.clientWidth, h: d.body.clientHeight }; 349 } 350 351 GLOBAL.TOOL.getPosition=function(element) { 352 if(element.getBoundingClientRect) { 353 var scrollOffsets = GLOBAL.TOOL.getScrollOffsets(); 354 var box = element.getBoundingClientRect(); 355 return{x:box.left + scrollOffsets.x,y:box.top + scrollOffsets.y}; 356 } 357 var xPosition = 0; 358 var yPosition = 0; 359 360 while(element) { 361 xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft); 362 yPosition += (element.offsetTop - element.scrollTop + element.clientTop); 363 element = element.offsetParent; 364 } 365 return { x: xPosition, y: yPosition }; 366 } 367 368 GLOBAL.TOOL.getWindowPosition = function(element) { 369 if (element.getBoundingClientRect) { 370 var box = element.getBoundingClientRect(); 371 return { 372 x: box.left, 373 y: box.top 374 } 375 } 376 var xPosition = 0; 377 var yPosition = 0; 378 while (element) { 379 xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft); 380 yPosition += (element.offsetTop - element.scrollTop + element.clientTop); 381 element = element.offsetParent 382 } 383 return { 384 x: xPosition, 385 y: yPosition 386 } 387 } 388 389 GLOBAL.TOOL.parsePx=function(paramter) { 390 var res = 0; 391 if (typeof(paramter) == "string" && paramter != null && paramter != "" ) { 392 var p = paramter.indexOf("px"); 393 if (p >= 0) { 394 res = parseInt(paramter.substring(0, p)); 395 } 396 } 397 return res; 398 } 399 /*事件操作*/ 400 GLOBAL.namespace("Event"); 401 402 /*获取事件对象*/ 403 GLOBAL.Event.getEventTarget=function(e){ 404 e = window.event||e; 405 return e.srcElement||e.target; 406 } 407 408 GLOBAL.Event.getEvent = function(e) 409 { 410 return e ? e : window.event; 411 } 412 /*事件冒泡*/ 413 GLOBAL.Event.stopPropagation=function(e) 414 { 415 e = window.event||e; 416 if(document.all) 417 { 418 e.cancelBubble = true; 419 } 420 else 421 { 422 e.stopPropagation(); 423 } 424 } 425 426 GLOBAL.Event.preventDefault = function(e) 427 { 428 if (e.preventDefault){ 429 e.preventDefault(); 430 } else { 431 e.returnValue = false; 432 } 433 } 434 /*添加事件*/ 435 GLOBAL.Event.on=function(node,eventType,handler,scope){ 436 node = typeof node == "string" ? document.getElementById(node) : node; 437 scope = scope || node; 438 var callHandler = function() { 439 handler.apply(scope, arguments); 440 }; 441 if (document.all) { 442 node.attachEvent("on" + eventType, callHandler); 443 } else { 444 node.addEventListener(eventType, callHandler, false); 445 } 446 return callHandler; 447 } 448 449 GLOBAL.Event.remove = function(node, eventType, handler) { 450 node = typeof node == "string" ? document.getElementById(node) : node; 451 if (document.all) { 452 node.detachEvent("on" + eventType, handler); 453 } else { 454 node.removeEventListener(eventType, handler, false); 455 } 456 } 457 458 /*单击与双击共存功能*/ 459 GLOBAL.Event.EventClick = function(scopeclick,callclick,scopedbclick,calldbclick) 460 { 461 this._dcTime=250; 462 this._dcAt=0; 463 this._savEvtTime=0; 464 this._savTO=null; 465 this._scopeclick = scopeclick; 466 this._callclick = callclick; 467 this._scopedbclick = scopedbclick; 468 this._calldbclick = calldbclick; 469 } 470 GLOBAL.Event.EventClick.prototype.click = function(ev) 471 { 472 if(this._savEvtTime - this._dcAt <=0) 473 { 474 return false; 475 } 476 /*执行单击事件*/ 477 this._callclick.apply(this._scopeclick,ev[0]); 478 } 479 GLOBAL.Event.EventClick.prototype.dbclick = function(ev) 480 { 481 var d = new Date(); 482 this._dcAt = d.getTime(); 483 if(this._savTO!=null){ 484 GLOBAL.Timer.clearTimeout(this._savTO); 485 } 486 /*执行双击事件*/ 487 this._calldbclick.apply(this._scopedbclick,ev); 488 } 489 490 GLOBAL.Event.EventClick.prototype.handleEvent =function(ev) { 491 ev = window.event||ev; 492 switch (ev.type) { 493 case "click" : 494 var d = new Date(); 495 this._savEvtTime = d.getTime(); 496 var paramter = []; 497 paramter[0] = ev; 498 this._savTO = GLOBAL.Timer.setTimeout(this.click,paramter,this._dcTime); 499 break; 500 case "dblclick": 501 this.dbclick(ev); 502 break; 503 default: 504 } 505 } 506 507 508 GLOBAL.Event.getSelectText = function() 509 { 510 var text; 511 if (window.getSelection) { 512 text = window.getSelection(); 513 return text; 514 } 515 else if (document.getSelection) { 516 text = document.getSelection(); 517 return text; 518 } 519 else { 520 var selection = document.selection && document.selection.createRange(); 521 text = selection.text; 522 523 return text; 524 } 525 526 }
时间: 2024-10-12 18:41:50