常用js方法集合,动态加载js方法--判断变量是否为空--截取小数点后几位--截取带中文的字条串
/*
* 动态加载外部js文件
*/
loadJS: function(url,id){ if(typeof(id)=='undefined'){ id = new Date().getTime(); } var scriptTag = document.getElementById( id ); var oHead = document.getElementsByTagName('HEAD').item(0); var oScript= document.createElement("script"); if ( scriptTag ) oHead.removeChild( scriptTag ); oScript.id = id; oScript.type = "text/javascript"; oScript.src=url ; oHead.appendChild( oScript); }
// 初始化QQ聊天 initQQDailog: function (qq) { var html = "<a target=\"_blank\" href=\"http://wpa.qq.com/msgrd?v=3&uin=" + qq + "&site=qq&menu=yes\">" + "<img border=\"0\" src=\"http://wpa.qq.com/pa?p=2:" + qq + ":52&v=" + new Date().getTime() + "\" alt=\"点击这里给我发消息\" title=\"点击这里给我发消息\"/>" + "</a>"; return html; }
/* * 判断值是否为空 **/ isEmpty: function (obj) { if (obj == '' || obj == null || obj == undefined) { return true; } if (typeof(obj) == 'object') { for (var key in obj) { return false; } return true; } return false; }
/* * 拼接对象 */ concatObj: function () { var output = {}; for (var i in arguments) { for (var key in arguments[i]) { output[key] = arguments[i][key]; } } return output; }
/* * 把对象转换成url参数 */ obj2Para: function (obj) { var para = ""; $.each(obj, function (i, v) { para += "&" + i + "=" + v; }); return para.substr(1); }
/* * js进行模版替换 */ fetchView: function(strView,objData){ var matches = strView.match(/<!--\$\w+-->/g); if(matches != null){ matches.forEach(function(v){ var key = String(v.match(/\w+/)); strView = strView.replace(v,objData[key]); }); } var matches = strView.match(/\{\$\w+\}/g); if(matches != null){ matches.forEach(function(v){ var key = String(v.match(/\w+/)); strView = strView.replace(v,objData[key]); }); } return strView; }
chkEmail: function(strEmail) { if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(strEmail)) { return false; } else { return true; } }
/* * 截取小数点后几位 */ subNumber : function(number, digit){ var digit = digit || 2; var numberStr = number.toString(); var index = numberStr.indexOf("."); if(index == -1){ return numberStr; }else{ return numberStr.substring(0, index + digit + 1); } }
/* * 截取带中文的字条串 utf-8 */ sub_str : function (str, len) { var strlen = 0; var s = ""; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 128) { strlen += 2; } else { strlen++; } s += str.charAt(i); if (strlen >= len) { return s + '…'; } } return s; }
/* * 按住shift选择一个范围内的checkbox */ shiftPitch: function(pMark){ var runthis = this; runthis.checkBoxObj = ''; runthis.shiftDown = false; runthis.num = 1; //checkbox初始化 $('body').find(pMark+' tbody input[type=checkbox]').each(function(){ $(this).data('num',runthis.num++); }); //checkbox选中事件 $('body').find(pMark+' input[type=checkbox]:checkbox').change(function(){ if($(this).parent().parent().parent().find('input:checkbox:checked').length>0){ $(this).parent().parent().parent().siblings('thead').find('input[type=checkbox]').attr('checked',true); }else if($(this).parent().parent().parent().find('input:checkbox:checked').length<=0){ $(this).parent().parent().parent().siblings('thead').find('input[type=checkbox]').attr('checked',false); } if($(this).is(':checkbox:checked') && typeof(runthis.checkBoxObj) == 'object' && runthis.shiftDown == true){ var oldNum = parseInt(runthis.checkBoxObj.data('num')); var runNum = parseInt($(this).data('num')); var small = oldNum>runNum?runNum:oldNum; var big = oldNum>runNum?oldNum:runNum; $(this).parents(pMark).find('tbody input[type=checkbox]').each(function(){ if(parseInt($(this).data('num'))>=small && parseInt($(this).data('num'))<=big){ $(this).attr('checked',true); $(this).parents('tr').addClass('selected'); } }); } runthis.checkBoxObj = $(this); })