摘要:通用js文件,部分方法需引入jBox插件( jBox 是一款基于 jQuery 的多功能对话框插件)、select2组件
1 /*! 2 * 3 * 通用公共方法 4 * @author caojianfeng 5 * @version 2014-4-29 6 */ 7 $(document).ready(function() { 8 try{ 9 // 链接去掉虚框 10 $("a").bind("focus",function() { 11 if(this.blur) {this.blur()}; 12 }); 13 // 所有下拉框使用select2 14 $("select").select2(); 15 }catch(e){ 16 // blank 17 } 18 }); 19 20 // 引入js和css文件 21 function include(id, path, file){ 22 if (document.getElementById(id)==null){ 23 var files = typeof file == "string" ? [file] : file; 24 for (var i = 0; i < files.length; i++){ 25 var name = files[i].replace(/^\s|\s$/g, ""); 26 var att = name.split('.'); 27 var ext = att[att.length - 1].toLowerCase(); 28 var isCSS = ext == "css"; 29 var tag = isCSS ? "link" : "script"; 30 var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' "; 31 var link = (isCSS ? "href" : "src") + "='" + path + name + "'"; 32 document.write("<" + tag + (i==0?" id="+id:"") + attr + link + "></" + tag + ">"); 33 } 34 } 35 } 36 37 // 获取URL地址参数 38 function getQueryString(name, url) { 39 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 40 if (!url || url == ""){ 41 url = window.location.search; 42 }else{ 43 url = url.substring(url.indexOf("?")); 44 } 45 r = url.substr(1).match(reg) 46 if (r != null) return unescape(r[2]); return null; 47 } 48 49 //获取字典标签 50 function getDictLabel(data, value, defaultValue){ 51 for (var i=0; i<data.length; i++){ 52 var row = data[i]; 53 if (row.value == value){ 54 return row.label; 55 } 56 } 57 return defaultValue; 58 } 59 //通过类型和值获取在中文名称 60 function getDictLabelByType(type, value){ 61 if (type && value) { 62 var dictLabel = cookie(type + "_" + value); 63 if(!dictLabel){ 64 $.ajax({ 65 url: ctx + "/sys/dict/getDictLabel", 66 type : 'post', 67 data : {type : type, value : value}, 68 async: false, 69 success:function(result){ 70 dictLabel = result; 71 cookie(type + "_" + value, dictLabel); 72 return dictLabel; 73 } 74 }); 75 } 76 return dictLabel; 77 } 78 return value; 79 } 80 81 // 打开一个窗体 82 function windowOpen(url, name, width, height){ 83 var top=parseInt((window.screen.height-height)/2,10),left=parseInt((window.screen.width-width)/2,10), 84 options="location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,"+ 85 "resizable=yes,scrollbars=yes,"+"width="+width+",height="+height+",top="+top+",left="+left; 86 window.open(url ,name , options); 87 } 88 89 // 恢复提示框显示 90 function resetTip(){ 91 top.$.jBox.tip.mess = null; 92 } 93 94 // 关闭提示框 95 function closeTip(){ 96 top.$.jBox.closeTip(); 97 } 98 99 //显示提示框 100 function showTip(mess, type, timeout, lazytime){ 101 resetTip(); 102 setTimeout(function(){ 103 top.$.jBox.tip(mess, (type == undefined || type == '' ? 'info' : type), {opacity:0, 104 timeout: timeout == undefined ? 2000 : timeout}); 105 }, lazytime == undefined ? 500 : lazytime); 106 } 107 108 // 显示加载框 109 function loading(mess){ 110 if (mess == undefined || mess == ""){ 111 mess = "正在提交,请稍等..."; 112 } 113 resetTip(); 114 top.$.jBox.tip(mess,'loading',{opacity:0}); 115 } 116 117 // 关闭提示框 118 function closeLoading(){ 119 // 恢复提示框显示 120 resetTip(); 121 // 关闭提示框 122 closeTip(); 123 } 124 125 // 警告对话框 126 function alertx(mess, closed){ 127 top.$.jBox.info(mess, '提示', {closed:function(){ 128 if (typeof closed == 'function') { 129 closed(); 130 } 131 }}); 132 top.$('.jbox-body .jbox-icon').css('top','38px'); 133 } 134 135 //错误对话框 add chq 20170502 136 function errorx(mess, closed){ 137 top.$.jBox.error(mess, '错误', {closed:function(){ 138 if (typeof closed == 'function') { 139 closed(); 140 } 141 }}); 142 top.$('.jbox-body .jbox-icon').css('top','55px'); 143 } 144 145 // 确认对话框 146 function confirmx(mess, href, closed){ 147 top.$.jBox.confirm(mess,'系统提示',function(v,h,f){ 148 if(v=='ok'){ 149 if (typeof href == 'function') { 150 href(); 151 }else{ 152 resetTip(); //loading(); 153 location = href; 154 } 155 } 156 },{buttonsFocus:1, closed:function(){ 157 if (typeof closed == 'function') { 158 closed(); 159 } 160 }}); 161 top.$('.jbox-body .jbox-icon').css('top','38px'); 162 return false; 163 } 164 165 // 提示输入对话框 166 function promptx(title, lable, href, closed){ 167 top.$.jBox("<div class='form-search' style='padding:20px;text-align:center;'>" + lable + ":<input type='text' id='txt' name='txt'/></div>", { 168 title: title, submit: function (v, h, f){ 169 if (f.txt == '') { 170 top.$.jBox.tip("请输入" + lable + "。", 'error'); 171 return false; 172 } 173 if (typeof href == 'function') { 174 href(); 175 }else{ 176 resetTip(); //loading(); 177 location = href + encodeURIComponent(f.txt); 178 } 179 },closed:function(){ 180 if (typeof closed == 'function') { 181 closed(); 182 } 183 }}); 184 return false; 185 } 186 187 // 页面跳转 188 function go(url){ 189 location.href = url; 190 } 191 function goWithTips(url){ 192 resetTip(); 193 go(url); 194 } 195 196 // cookie操作 197 function cookie(name, value, options) { 198 if (typeof value != 'undefined') { // name and value given, set cookie 199 options = options || {}; 200 if (value === null) { 201 value = ''; 202 options.expires = -1; 203 } 204 var expires = ''; 205 if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { 206 var date; 207 if (typeof options.expires == 'number') { 208 date = new Date(); 209 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); 210 } else { 211 date = options.expires; 212 } 213 expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE 214 } 215 var path = options.path ? '; path=' + options.path : ''; 216 var domain = options.domain ? '; domain=' + options.domain : ''; 217 var secure = options.secure ? '; secure' : ''; 218 document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); 219 } else { // only name given, get cookie 220 var cookieValue = null; 221 if (document.cookie && document.cookie != '') { 222 var cookies = document.cookie.split(';'); 223 for (var i = 0; i < cookies.length; i++) { 224 var cookie = jQuery.trim(cookies[i]); 225 // Does this cookie string begin with the name we want? 226 if (cookie.substring(0, name.length + 1) == (name + '=')) { 227 cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 228 break; 229 } 230 } 231 } 232 return cookieValue; 233 } 234 } 235 236 // 数值前补零 237 function pad(num, n) { 238 var len = num.toString().length; 239 while(len < n) { 240 num = "0" + num; 241 len++; 242 } 243 return num; 244 } 245 246 // 转换为日期 247 function strToDate(date){ 248 return new Date(date.replace(/-/g,"/")); 249 } 250 251 // 日期加减 252 function addDate(date, dadd){ 253 date = date.valueOf(); 254 date = date + dadd * 24 * 60 * 60 * 1000; 255 return new Date(date); 256 } 257 258 //截取字符串,区别汉字和英文 259 function abbr(name, maxLength){ 260 if(!maxLength){ 261 maxLength = 20; 262 } 263 if(name==null||name.length<1){ 264 return ""; 265 } 266 var w = 0;//字符串长度,一个汉字长度为2 267 var s = 0;//汉字个数 268 var p = false;//判断字符串当前循环的前一个字符是否为汉字 269 var b = false;//判断字符串当前循环的字符是否为汉字 270 var nameSub; 271 for (var i=0; i<name.length; i++) { 272 if(i>1 && b==false){ 273 p = false; 274 } 275 if(i>1 && b==true){ 276 p = true; 277 } 278 var c = name.charCodeAt(i); 279 //单字节加1 280 if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { 281 w++; 282 b = false; 283 }else { 284 w+=2; 285 s++; 286 b = true; 287 } 288 if(w>maxLength && i<=name.length-1){ 289 if(b==true && p==true){ 290 nameSub = name.substring(0,i-2)+"..."; 291 } 292 if(b==false && p==false){ 293 nameSub = name.substring(0,i-3)+"..."; 294 } 295 if(b==true && p==false){ 296 nameSub = name.substring(0,i-2)+"..."; 297 } 298 if(p==true){ 299 nameSub = name.substring(0,i-2)+"..."; 300 } 301 break; 302 } 303 } 304 if(w<=maxLength){ 305 return name; 306 } 307 return nameSub; 308 }