分享

常用js

 血色残阳梦 2011-04-13

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;  
  }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约