(function(mod) { if (typeof exports == "object" && typeof module == "object") // commonjs 规范 module.exports = mod; else if (typeof define == "function" && define.amd) // amd 规范 return define(['jquery','jquery.validate'], mod); else // plain browser env 浏览器 this.tools = mod(); })(function() { var tools = {}; tools.check_cookie = function(){ if(window.navigator.cookieenabled) return true; else{ alert("浏览器配置错误,cookie不可用!"); return false; } }; tools.set_cookie = function(name,value){ var days = 30; //此 cookie 将被保存 30 天 var exp = new date(); //new date("december 31, 9998"); exp.settime(exp.gettime() + days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.togmtstring(); }; tools.get_cookie = function(name) { var arr = document.cookie.match(new regexp("(^| )"+name+"=([^;]*)(;|$)")); if(arr !== null){ return unescape(arr[2]); }else{ return null; } // var regexp = new regexp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g"); // var result = regexp.exec(document.cookie); // return (result === null) ? null : result[1]; }; tools.del_cookie = function(name) { var exp = new date(); exp.settime(exp.gettime() - 1); var cval=getcookie(name); if(cval!==null){ document.cookie= name + "="+cval+";expires="+exp.togmtstring(); } }; // 检测空对象 // 空则返回true tools.isemptyvalue = function(value) { var type; if(value === null) { // 等同于 value === undefined || value === null return true; } type = object.prototype.tostring.call(value).slice(8, -1); switch(type) { case 'string': return !!$.trim(value); case 'array': return !value.length; case 'object': // return $.isemptyobject(value); return !value.length; default: return false; } }; tools.isempty = function(v){ if (v instanceof string) { var r = /^\s*$/; return r.test(v); }else if(v instanceof array){ return !v.length; }else if(v instanceof object){ return v === {}; }else{ return false; } }; tools.is_set = function(value){ if (typeof(value) == "undefined") { return false; }else{ return true; } }; // 解析url路径 tools.parseurl = function(url) { var a = document.createelement('a'); a.href = url; return { source: url, protocol: a.protocol.replace(':',''), host: a.hostname, port: a.port, query: a.search, params: (function(){ var ret = [], seg = a.search.replace(/^\?/,'').split('&'), len = seg.length, i = 0, s; for (;i 1024 ) { ready.call(img); onready.end = true; } }; onready(); // 完全加载完毕的事件 img.onload = function () { // onload在定时器时间差范围内可能比onready快 // 这里进行检查并保证onready优先执行 !onready.end && onready(); load && load.call(img); // ie gif动画会循环执行onload,置空onload即可 img = img.onload = img.onerror = null; }; // 加入队列中定期执行 if (!onready.end) { list.push(onready); // 无论何时只允许出现一个定时器,减少浏览器性能损耗 if (intervalid === null) intervalid = setinterval(tick, 40); }; }; })(); tools.load_page_with_data = function(_url,nodeid,dataobj) { var _load_page = function(__url){ $.ajax({ url: __url, type: 'get', data: dataobj, datatype: 'html' }) .done(function(data) { console.log("success"); $(nodeid).html(data); $(nodeid+" .pagination a").each(function(index, el) { var elhref = $(el).attr('href'); if ( elhref == '#' ) { elhref = __url; $(el).attr('data-href',__url); }else{ $(el).attr('data-href',elhref); } $(el).attr('href','javascript:void(0)'); // $(el).attr('href',"javascript:tools.load_page('"+elhref+"','"+nodeid+"')"); }); }) .fail(function() { // console.log("error"); }) .always(function() { // console.log("complete"); }); }; _load_page(_url); $(nodeid).undelegate('a[data-href]' , 'click').delegate('a[data-href]','click',function(){ _load_page($(this).attr('data-href')); }); }; // 一部加载页面对分页按钮处理 ,id 带有 # tools.load_page = function(_url,nodeid) { var _load_page = function(__url){ $.ajax({ url: __url, type: 'get', datatype: 'html', beforesend:function(){ $('.product-list-page .load-view').stop().show(); }, success:function(data){ $(nodeid).html(data); $(nodeid+" .pagination a").each(function(index, el) { var elhref = $(el).attr('href'); if ( elhref == '#' ) { // elhref = __url; $(el).attr('data-href',__url); }else{ $(el).attr('data-href',elhref); } $(el).attr('href','javascript:void(0)'); // $(el).attr('href',"javascript:tools.load_page('"+elhref+"','"+nodeid+"')"); }); if($('.recruit-box').length>0){ $('.recruit-list .recruit-item').eq(0).addclass('cur'); $('.recruit-list .recruit-item').eq(0).find('.recruit-drop').stop().show(); } $('.product-list-page .load-view').stop().hide(); } }) // .done(function(data) { // console.log("success"); // }) .fail(function() { // console.log("error"); }) .always(function() { // console.log("complete"); }); }; _load_page(_url); //bug修复20190313 //$(nodeid).delegate('a[data-href]','click',function(){ $(nodeid).off("click").on("click",'a[data-href]',function(){ _load_page($(this).attr('data-href')); }); }; /** * @brief 创建表单验证 * 注意引用 jquery.validate.js 和 jquery.validate.lang.cn.js * @param frm 表单 id * @param rules 验证规则 */ tools.make_validate = function(frm,rules,messages,inline){ if (inline === 0) { inline = "block"; }else{ inline = "inline"; } $('#'+frm).validate({ ignore: ".ignore", // 忽略 rules:rules, onchange: true, onblur: true, messages:messages, errorclass: "help-"+inline+" validate", errorelement: "span", highlight:function(element, errorclass, validclass) { $(element).parents('.control-group').removeclass('success').addclass('error'); $(element).removeclass('success').addclass('error'); $(element).nextall('span.validate').remove(); }, unhighlight: function(element, errorclass, validclass) { $(element).parents('.control-group').removeclass('error').addclass('success'); $(element).removeclass('error').addclass('success'); $(element).nextall('span.validate').remove(); }, errorplacement: function(error, element) { if (inline == "block") { error.appendto(element.parents(".control-group")); }else{ element.after(error); } } }); }; /** * @brief 创建表单验证 带有返回函数, 可以验证后直接用ajax处理 * 注意引用 jquery.validate.js 和 jquery.validate.lang.cn.js * @param frm 表单 id * @param rules 验证规则 */ tools.make_validate_submit = function(frm,rules,messages,submit_fun,inline){ if (inline === 0) { inline = "block"; }else{ inline = "inline"; } $('#'+frm).validate({ ignore: ".ignore", // 忽略 rules:rules, onchange: true, onblur: true, messages:messages, errorclass: "help-inline validate", errorelement: "span", highlight:function(element, errorclass, validclass) { $(element).parents('.control-group').removeclass('success').addclass('error'); $(element).removeclass('success').addclass('error'); $(element).nextall('span.validate').remove(); }, unhighlight: function(element, errorclass, validclass) { $(element).parents('.control-group').removeclass('error').addclass('success'); $(element).removeclass('error').addclass('success'); $(element).nextall('span.validate').remove(); }, errorplacement: function(error, element) { if (inline == "block") { error.appendto(element.parents(".control-group")); }else{ element.after(error); } }, submithandler:function(form){ submit_fun(form); } }); }; return tools; });