【JavaScript】分享一个我自己写的小工具类!
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【JavaScript】分享一个我自己写的小工具类!,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5571字,纯文字阅读大概需要8分钟。
内容图文
![【JavaScript】分享一个我自己写的小工具类!](/upload/InfoBanner/zyjiaocheng/1118/4b69b1a4398f46d79766c2f6e47f921f.jpg)
1 function Alpha(){ 2 3 console.log(‘alpha:hello‘); 4 5this.lastclick = 0; 6this.hideNode = {}; 7 8// 测试调用 9this.hello = function (){console.log(‘Hello, you called me.‘);} 10 11// 返回包含数组中所有键名的一个新数组 12this.array_keys = function (arr) { 13var RET = Array(), i = 0; 14for (var key in arr) {RET[i] = key;i++;} 15return RET;} 16 17// 判断是否数组 18this.isarray = function (arg) { 19if (arg === null) returnfalse; 20if (typeof (arg) == "object") returntrue; 21returnfalse;} 22 23// 页面跳转 24this.gocpage = function (cpage) { 25var ipage = document.getElementById(‘ipage‘); 26if (empty(ipage)) ipage = parent.document.getElementById(‘ipage‘); 27 ipage.setAttribute("src", cpage.id + ".html" + suffix()); 28function suffix() {return "?t=" + new Date().getTime();}} 29 30// 若变量已存在、非空字符串或者非零,则返回 false 值;反之返回 true。 31this.empty = function (arg) { 32if (arg === null) returntrue; 33var argtype = typeof (arg); 34switch (argtype) { 35case "undefined":returntrue; 36case "object":if (arg.length == 0) returntrue;returnfalse; 37case "function":returnfalse; 38case "boolean":if (arg === false) returntrue;returnfalse; 39case "number":if (arg == 0) returntrue;returnfalse; 40case "string":if (arg.length == 0) returntrue;returnfalse; 41default:returnfalse;}} 42 43// 隐藏自己 44this.hideself = function (node) {node.style.display = ‘none‘;} 45 46// 域名验证 47this.vali_domain = function (str) { 48var regdomain = /^[\w-]+\.[\w]+$|^[\w-]+\.[\w-]+\.[\w]+$|^[\w-]+\.[\w-]+\.[\w-]+\.[\w]+$/; 49if (regdomain.test(str)) returntrue; 50returnfalse;} 51 52// 单击 vs 双击,传入2个回调函数,用户点击时自动触发 53this.clickcall = function (singleclick, doubleclick) { 54var clicktime = new Date().getTime(); 55if (this.lastclick == 0) { 56this.lastclick = clicktime; 57 window.setTimeout(cc, 360);} 58else { // 双击 59if ((clicktime - this.lastclick) < 300) { 60this.lastclick = 0; 61 doubleclick();}} 62function cc() { // 单击 63if (this.lastclick == 0) returnfalse; 64this.lastclick = 0; 65 singleclick();}} 66 67// 返回url参数对象 68this.geturlarg = function () { 69var urlarg = new Object(); 70var t_urlarg = document.URL.split(‘?‘); 71if (empty(t_urlarg[1])) returnnull; 72var tt_urlarg = t_urlarg[1].split(‘&‘); 73for (var i = 0; !empty(tt_urlarg[i]); i++) { 74var ttt_urlarg = tt_urlarg[i].split(‘=‘); 75if (empty(ttt_urlarg[0])) continue; 76 urlarg[ttt_urlarg[0]] = empty(ttt_urlarg[1]) ? null : ttt_urlarg[1];} 77return urlarg;} 78 79// 鼠标覆盖 80this.mover = function (node) {node.style.background = ‘#CCCCCC‘;} 81 82// 鼠标离开 83this.mout = function (node) {node.style.background = ‘#ffffff‘;} 84 85// 获取字符串中的数字 86this.sel_num = function (str) { 87var regnum = /\d+/; 88var res = regnum.exec(str); 89return res[0];} 90 91// 计算2点间的距离 92this.cal_between = function (p1x, p1y, p2x, p2y) {return Math.sqrt(Math.pow(p1x - p2x, 2) + Math.pow(p1y - p2y, 2));} 93 94// 计算区域的中心 95this.cal_center = function (l, t, w, h) { 96var RES = new Object(); 97 RES[‘x‘] = l + w / 2; 98 RES[‘y‘] = t + h / 2; 99return RES;} 100101// ajax GET102this.get = function (url,callback){ 103if(this.empty(url)){console.log(‘url is undefined‘);return;} 104if(this.empty(callback)){console.log(‘callback is undefined‘);return;} 105var xmlhttp; 106if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); 107else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 108 xmlhttp.onreadystatechange=function(){ 109if(xmlhttp.readyState==4 && xmlhttp.status==200){ 110 callback(eval(‘(‘ + xmlhttp.responseText + ‘)‘));}} 111 xmlhttp.open("GET",url,true); 112 xmlhttp.send();} 113114// ajax POST115this.post = function (url,callback,arg){ 116if(this.empty(url)){console.log(‘url is undefined‘);return;} 117if(this.empty(callback)){console.log(‘callback is undefined‘);return;} 118var xmlhttp; 119if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); 120else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 121 xmlhttp.onreadystatechange=function(){ 122if(xmlhttp.readyState==4 && xmlhttp.status==200){ 123 callback(eval(‘(‘ + xmlhttp.responseText + ‘)‘));}} 124 xmlhttp.open("POST",url,true); 125 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 126if(this.empty(arg)) xmlhttp.send(); 127else xmlhttp.send(arg);} 128129// 隐藏body内的元素,呈现加载中130// 使用此方法需要加载 spin.js131this.loading = function(){ 132var spinmaxheight = window.screen.height; 133var spinmaxwidth = window.screen.width; 134var spintop = spinmaxheight/2; 135var spinleft = spinmaxwidth/2; 136if (spintop>100) spintop = 100; 137var spinfooo = document.createElement(‘div‘); 138 spinfooo.id = ‘spinfooo‘; 139var spinfooop = document.createElement(‘div‘); 140 spinfooop.appendChild(spinfooo); 141 spinfooop.style.position = ‘absolute‘; 142 spinfooop.style.top = spintop + ‘px‘; 143 spinfooop.style.left = spinleft + ‘px‘; 144var opts = { 145 lines: 5,length: 12,width: 4,radius: 12,corners: 0.2, 146 rotate: 15,color: ‘#000‘,speed: 1.1,trail: 20,shadow: false,hwaccel: false, 147 className: ‘spinner‘,zIndex: 2e9,top: ‘auto‘,left: ‘auto‘}; 148for(var i=0,tmp;;){ 149 tmp = document.body.children[i]; 150if (typeof(tmp) === ‘undefined‘) break; 151if (tmp.style.display == ‘none‘) continue; 152 tmp.style.display = ‘none‘; 153this.hideNode[i++] = tmp;} 154 document.body.appendChild(spinfooop); 155var spinner = new Spinner(opts).spin(spinfooo);} 156157// 呈现body内的元素,隐藏加载中158this.loaded = function(){ 159for(var i=0;;i++){ 160if (typeof(this.hideNode[i]) === ‘undefined‘) break; 161this.hideNode[i].style.display = ‘‘;} 162 document.getElementById(‘spinfooo‘).style.display = ‘none‘;} 163 }
原文:http://www.cnblogs.com/myaliyun/p/6275363.html
内容总结
以上是互联网集市为您收集整理的【JavaScript】分享一个我自己写的小工具类!全部内容,希望文章能够帮你解决【JavaScript】分享一个我自己写的小工具类!所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。