功能性代码
/* cleanWhitespace 返回第一层子节点数组 参数: node 要处理的文本节点 deep 是否处理元素的所有子辈节点 默认为 false del 删除空白节点 默认为 true */ function cleanWhitespace(node,deep,del){ var notWhitespace = /\S/; var returnNodes = []; // 返回第一层子节点数组 if(typeof deep == 'undefined' || deep == false){ var deep = false; }else{ var del = true; } if(typeof del == 'undefined' || del){ var del = true; } var childNodes = node.childNodes; for (var i=0; i < childNodes.length; i++) { var childNode = childNodes[i]; if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) { del && node.removeChild(childNodes[i]) && i--; continue; } if (childNode.nodeType == 1) { returnNodes.push(childNode); deep && cleanWhitespace(childNode, true, true); }; } return returnNodes || 'undefined'; }
判断数据类型的方法: 来源(http://www.css88.com/archives/1824)
1 function getType(o) {
2 var _t;
3 return (
4 (_t =typeof(o)) =="object"?
5 o==null&&"null"|| Object.prototype.toString.call(o).slice(8,-1):
6 _t
7 ).toLowerCase();
8 }
9
10 alert(getType("abc")); //string
11 alert(getType(true)); //boolean
12 alert(getType(123)); //number
13 alert(getType([])); //array
14 alert(getType({})); //object
15 alert(getType(function(){})); //function
16 alert(getType(new Date)); //date
17 alert(getType(new RegExp)); //regexp
18 alert(getType(Math)); //math
19 alert(getType(null)); //null
1 /*
2 QueryParam 方法 获取和设置地址栏的参数
3 参数 如果没有参数则返回当前地址栏的所有参数名值对象
4 参数类型一 : '如果没有参数则返回当前地址栏的所有参数名值对象'
5 参数类型二 :str '只有一参数字符串,则返回该参数的值'
6 参数类型三 :object
7 {
8 '如果没有参数则返回当前地址栏的所有参数名值对象',
9 key : '要获取的参数名的值',
10 |---value : '设置或修改key的值,则设置 key(必须有) 的值,并返回整个 location新的search 对象',
11 query_str: '要获取和操作的url对象的search 注意是search值'
12 }
13 */
14 function QueryParam(param){
15 var opts = {};
16 if(typeof param ==='string'){// 直接字符串读取
17 opts.key = param;
18 }elseif(typeof param ==='undefined'){
19 var opts = {};
20 }else{
21 var opts = param || {};
22 }
23 var query_str = opts.query_str || window.location.search;
24 if(query_str.length ==0){return};
25
26 if(opts.key !== undefined){
27
28 // 如果设置值,并返回新的 query_str
29 if(opts.value !== undefined ){
30 var REP=new RegExp("([\?\&]"+ opts.key +"=).+?($|\&)");
31
32 if(!REP.test(query_str)){ // 如果不存在就写入
33 return query_str +'&'+ opts.key +'='+ escape(opts.value);
34 };
35
36 return query_str.replace(REP,"$1"+ escape(opts.value) +"$2");
37 }
38
39 //返回请求的参数值
40 var REP=new RegExp("([\&\?\^]"+ opts.key +"=)(.+?)($|\&)");
41 var value = query_str.match(REP);
42 return value && unescape(value[2]);
43 }
44
45 //返回当前 search 的所有参数名值对象
46 var objParam = {};
47 var param = query_str.replace(/^\?/,'').split('&');
48 for(var i =0, j = param.length; i < j; i++){
49 var x = param[i].split('=');
50 objParam[unescape(x[0])] = unescape(x[1]);
51 }
52
53 return objParam;
54 }
动态加载外面 js 文件
1 function creatScript(src,callBack){
2 var script = document.createElement("script");
3 script.setAttribute("src",src);
4 document.getElementsByTagName("head")[0].appendChild(script);
5
6 script.onload = script.onreadystatechange = function() {
7 if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete" ) {
8 callBack();
9 script.onload = script.onreadystatechange = null;
10 }
11 };
12 }
数字格式转换成千分位
1 /** 2 * commafy 3 * 数字格式转换成千分位 4 *@param{Object}num 5 */ 6 7 ;(function(win){ 8 win.commafy = { 9 "do" : function(num){ 10 if ((num + "").replace(/\s/,'') == "") {return ""; } 11 if (isNaN(num)) {return ""; } 12 13 num = num + ""; 14 if (/^.*\..*$/.test(num)) { 15 var pointIndex = num.lastIndexOf("."); 16 var intPart = num.substring(0, pointIndex); 17 var pointPart = num.substring(pointIndex + 1, num.length); 18 intPart = intPart + ""; 19 var re = /(-?\d+)(\d{3})/ 20 while (re.test(intPart)) { 21 intPart = intPart.replace(re, "$1,$2") 22 } 23 num = intPart + "." + pointPart; 24 }else{ 25 num = num + ""; 26 var re = /(-?\d+)(\d{3})/ 27 while (re.test(num)) { 28 num = num.replace(re, "$1,$2") 29 } 30 } 31 return num; 32 }, 33 "undo" : function(num) { 34 if ((num + "").replace(/\s/,'') == "") { 35 return ""; 36 } 37 num = num.replace(/,/gi, ''); 38 return num; 39 } 40 } 41 42 }(window));
方法二
1 /** 2 * 将数值四舍五入后格式化. 3 * @param num 数值(Number或者String) 4 * @param isThousand 是否需要千分位 (boolean); 5 * @param cent 要保留的小数位(Number) 6 * @return 格式的字符串,如'1,234,567.45' 7 * @type String 8 */ 9 function formatNumber(num,isThousand,cent) { 10 console.info(num,"---") 11 var num = num.toString().replace(/\$|\,/g,''); 12 var isThousand = isThousand === false ? false : true; 13 var cent = cent || 0; 14 // 检查传入数值为数值类型 15 if(isNaN(num)) 16 num = "0"; 17 18 // 获取符号(正/负数) 19 sign = (num == (num = Math.abs(num))); 20 21 num = Math.floor(num*Math.pow(10,cent)+0.50000000001); // 把指定的小数位先转换成整数.多余的小数位四舍五入 22 cents = num%Math.pow(10,cent); // 求出小数位数值 23 num = Math.floor(num/Math.pow(10,cent)).toString(); // 求出整数位数值 24 cents = cents.toString(); // 把小数位转换成字符串,以便求小数位长度 25 26 // 补足小数位到指定的位数 27 while(cents.length<cent) 28 cents = "0" + cents; 29 30 if(isThousand) { 31 // 对整数部分进行千分位格式化. 32 for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 33 num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); 34 } 35 36 if (cent > 0) 37 return (((sign)?'':'-') + num + '.' + cents); 38 else 39 return (((sign)?'':'-') + num); 40 }
判断ie的版本号
1 var _ie = (function() { 2 var v = 3, 3 div = document.createelement('div'), 4 all = div.getelementsbytagname('i'); 5 while ( 6 div.innerhtml = '<!--[if gt ie ' + (++v) + ']><i></i><![endif]-->', 7 all[0] 8 ); 9 return v > 4 ? v : false; 10 }());
限制频繁调用一个函数
1 function debounce(fun, wait, immediate){ 2 3 var timeoutId = null; 4 5 immediate = (immediate === undefined) ? true : immediate; 6 wait = wait || 200; 7 8 return function(){ 9 if(!timeoutId){ 10 var context = this, args = arguments; 11 immediate && fun.call(context, args); 12 timeoutId = setTimeout(function(){ 13 !immediate && fun.call(context, args); 14 clearTimeout(timeoutId); 15 timeoutId = null; 16 }, wait); 17 } 18 } 19 }