倒计时:
<span style="font-family:SimSun;font-size:10px;">/* 实现多个时间进行倒计时,或者直接格式化时间:time为时间戳 */ var deadline = { counting:function(ele,timer){ var _this=this; setTimeout(function(){ if(timer >=0 ){ ele.html(_this.transfer(timer-1)); _this.counting(ele,timer-1); } },1000) }, transfer:function(time){ var d=Math.floor(time/86400); var h=Math.floor((time-d*86400)/3600); var m=Math.floor((time-d*86400-h*3600)/60); var s=time-d*86400-h*3600-m*60; return d+'天'+h+'时'+m+'分'+s+'秒'; }, transferTime:function(time){/* 与当前时间比较,如果小于当前时间,就不显示 */ var now = new Date(), endDate = new Date(time*1000), leftTime = endDate.getTime()-now.getTime(), leftsecond = parseInt(leftTime/1000); if( leftsecond >0 ){ var date = new Date(time*1000); var year = date.getFullYear(); var month = date.getMonth()+1; month = month < 10 ? ( '0'+month ) : month; var day = date.getDate() < 10 ? ( '0'+date.getDate() ) : date.getDate(); var hour = date.getHours() < 10 ? ( '0'+date.getHours() ) : date.getHours(); var minute = date.getMinutes() < 10 ? ( '0'+date.getMinutes() ) : date.getMinutes(); var second = date.getSeconds() < 10 ? ( '0'+date.getSeconds() ) : date.getSeconds(); return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second; }else{ return ''; } } };</span>
金额千分位格式化:
amount.toString().replace(^/\d{1,3}(?=(\d{3})+(\.\d*)$)/g, '$&,');
js字体适应式函数:
function fixRem(){ var windowWidth = window.innderWidth || document.documentElement.clientWidth || document.body.clientWidth; windowWidth = windowWidth < 320 ? 320 : windowWidth ; //body min-width in css var rootSize = 20 * ( windowWidth / 360); rootSize = rootSize > 36 ? 36 : rootSize; var htmlNode = document.getElementsByTagName("html")[0]; htmlNode.style.fontSize = rootSize+'px'; }
css字体适应方式:
@min-font-size: 20px; @max-font-size: 36px; @min-screen: 360px; @max-screen: 800px; :root { font-size: @min-font-size; } @media (min-width:@min-screen) and (max-width:@max-screen) { :root { font-size: calc( @min-font-size + (@max-font-size - @min-font-size) * ((100vw - @min-screen) / (@max-screen - @min-screen))); } } @media (min-width:@max-screen) { :root { font-size: @max-font-size; } }
获取表单的json对象:
function getFormJson(form) { var o = {}; var a = $(form).serializeArray(); $.each(a, function() { if (!this.name) return true; if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }
获取object长度
function getPropertyCount(o){ var n, count = 0; for(n in o){ if(o.hasOwnProperty(n)){ count++; } } return count; }
Object.keys(obj).length
限制输入框只能输入数字(保留N位小数),不考虑粘贴
//获取当前光标在文本框的位置 function getCurPosition(domObj) { var position = 0; if (domObj.selectionStart || domObj.selectionStart == '0') { position = domObj.selectionStart; } else if (document.selection) { //for IE domObj.focus(); var currentRange = document.selection.createRange(); var workRange = currentRange.duplicate(); domObj.select(); var allRange = document.selection.createRange(); while (workRange.compareEndPoints("StartToStart", allRange) > 0) { workRange.moveStart("character", -1); position++; } currentRange.select(); } return position; } $.fn.decimalinput = function(num) { $(this).css("ime-mode", "disabled"); this.on("keypress", function(e) { if (e.charCode === 0) return true; //非字符键 for firefox var code = (e.keyCode ? e.keyCode : e.which); //兼容火狐 IE if (code >= 48 && code <= 57) { var pos = getCurPosition(this); var dotPos = this.value.indexOf("."); if ( code == 48 && pos == 0) { return false; } if (dotPos > 0 && pos > dotPos) { if (pos > dotPos + num) return false; if (this.value.substr(dotPos + 1).length < num) return true; else return false; } return true; } //输入"." if (code == 46) { if (/^[0-9]+\.$/.test(this.value + String.fromCharCode(code))) return true; } return false; }); this.on("input propertychange", function() { var value = this.value; if( value.substr(0,1)== 0 ){ value = value.substring(1, value.length); this.value = value; } var reg = new RegExp('(?:[1-9]\d*?(?:\.\d{0,'+num+'})?)$'); if( !reg.test(value)){ this.value = value.replace(/[^0-9\.]/g, ""); } }); this.on("paste dragenter contextmenu", function() { return false; }); };
$("#property_money").decimalinput(4);注意:
1. selectionStart对number input不起作用,好像只对input的type为text起作用
2. ime-mode:css属性,设置或检索是否允许用户激活输入中文,韩文,日文等的输入法(IME)状态。
参考:
http://www.qdfuns.com/notes/26716/6ada0d47a845cc2f581bd85d28d270c9.html
http://blog.csdn.net/huangyong19870618/article/details/5873044
http://www.cnblogs.com/nackman/archive/2013/01/28/2880154.html
eval
只在被直接调用并且调用函数就是 eval
本身时,才在当前作用域中执行。否者,其作用域都是全局。不建议使用eval。
//直接调用 var foo = 1; function test() { var foo = 2; eval('foo = 3'); return foo; } test(); // 3 foo; // 1 //非直接调用 var foo = 1; function test() { var foo = 2; var bar = eval; bar('foo = 3'); return foo; } test(); // 2 foo; // 3
setTimeout
和
setInterval
都可以接受字符串作为它们的第一个参数,它在内部使用了 eval
。 这个字符串
总是
在全局作用域中执行,因此
eval
在这种情况下没有被直接调用。建议第一个参数适用匿名函数。