1. js实现浮点数位数的保留
方法1:
/** *v表示要转换的值 *e表示要保留的位数 */ function round(v,e){ var t=1; for(;e>0;t*=10,e--); for(;e<0;t/=10,e++); return Math.round(v*t)/t; }
使用方法:value = round(value,2);//保留2位小数,并且可以格式化数字,在数字过大时不会出现科学计数法。
方法2:
value=parseFloat(value).toFixed(2).toString();//保留2位小数,并且可以格式化数字,在数字过大时不会出现科学计数法。
2. js日历控件的使用
引进js文件:
<script type="text/javascript" src="My97DatePicker/WdatePicker.js" defer="defer"></script>
使用:
1) 使用默认格式:及yyyy-MM-dd
<input type="text" name="date" onclick="WdatePicker()" value="" class="Wdate"/>
2) 指定格式:
如:yyyy-MM-dd或yyyy-MM或yyyy-MM-dd HH:mm:ss
<input type="text" name="date" onclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd HH:mm:ss'})" value="" class="Wdate"/>
说明:
skin:'whyGreen'设定控件的样式
class="Wdate" 设定输入框到的样式
注:
使用的时候需要日历控件的js文件的支持。
2. 关闭子窗口刷新父窗口
父窗口写法:
window.open("...");void 0;
子窗口写法:
执行完其他的操作(比如:提交表单form.submit,或window.location.href=""),
执行:window.opener.location=window.opener.location; this.close();
3. 将秒转换成小时分钟秒的js
function formatSeconds(value) { var theTime = Number(value); var theTime1 = 0; var theTime2 = 0; //alert(theTime); if(theTime > 60) { theTime1 = Number(theTime/60); theTime = Number(theTime%60); //alert(theTime1+"-"+theTime); if(theTime1 > 60) { theTime2 = Number(theTime1/60); theTime1 = Number(theTime%60); } } var result = ""+theTime+"s"; if(theTime1 > 0) { result = ""+parseInt(theTime1)+"m"+result; } if(theTime2 > 0) { result = ""+parseInt(theTime2)+"h"+result; } return result; }
|