我的js函数库(持续更新)

  1. 常用js初始化函数
    •  
      function id(obj) {
          return document.getElementById(obj);
      }
      function bind(obj, ev, fn) { 
          if (obj.addEventListener) {
              obj.addEventListener(ev, fn, false);
          } else {
              obj.attachEvent('on' + ev, function() {
                  fn.call(obj);
              });
          }
      }
      function view() {
          return {
              w: document.documentElement.clientWidth,
              h: document.documentElement.clientHeight
          };
      }
      function addClass(obj, sClass) { 
          var aClass = obj.className.split(' ');
          if (!obj.className) {
              obj.className = sClass;
              return;
          }
          for (var i = 0; i < aClass.length; i++) {
              if (aClass[i] === sClass) return;
          }
          obj.className += ' ' + sClass;
      }
      
      function removeClass(obj, sClass) { 
          var aClass = obj.className.split(' ');
          if (!obj.className) return;
          for (var i = 0; i < aClass.length; i++) {
              if (aClass[i] === sClass) {
                  aClass.splice(i, 1);
                  obj.className = aClass.join(' ');
                  break;
              }
          }
      }

       

  2. getByClass(oParent, sClass)
    •  1 function getByClass(oParent, sClass)
       2 {
       3 var aEle=oParent.getElementsByTagName('*');
       4 var aResult=[];
       5 var i=0;
       6 
       7 for(i=0;i<aEle.length;i++)
       8 {
       9 if(aEle[i].className==sClass)
      10 {
      11 aResult.push(aEle[i]);
      12 }
      13 }
      14 
      15 return aResult;
      16 }

       

  3. 获取样式  
    1. function getStyle(obj,attr)
      {
      if(obj.currentStyle)
      {
      return obj.currentStyle[attr];
      }
      else
      {
      return getComputedStyle(obj,false)[attr];
      }

       

  4. 完美运动框架
    • function getStyle(obj,attr)
      {
      if(obj.currentStyle)
      {
      return obj.currentStyle[attr];
      }
      else
      {
      return getComputedStyle(obj,false)[attr];
      }
      }
      function move(obj,json,fn)
      {
      clearInterval(obj.timer);
      
      obj.timer=setInterval(function(){
      var bStop=true;
      for(attr in json)
      {
      //设定初始值
      var iCur=0;
      if(attr=='opacity')
      {
      iCur=parseInt(parseFloat(getStyle(obj,attr))*100);
      }
      else
      {
      iCur=parseInt(getStyle(obj,attr));
      }
      //设定速度
      var iSpeed=(json[attr]-iCur)/8;
      iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
      if(iCur!==json[attr])
      {
      bStop=false;
      
      }
      
      if(attr=='opacity')
      {
      obj.style.filter="alpha(opacity:"+(iCur+iSpeed)+')';
      obj.style.opacity=(iCur+iSpeed)/100;
      }
      else
      {
      obj.style[attr]=iCur+iSpeed+'px';
      }
      }
      
      
      if(bStop)
      {
      clearInterval(obj.timer);
      
      if(fn)
      {
      fn();
      }
      }
      
      },30)
      }
  5. Js获取页面地址参数 

    代码如下:

    function getUrlPara(paraName) 
    { 
    var sUrl = location.href; 
    var sReg = "(?://?|&){1}" + paraName + "=([^&]*)" 
    var re = new RegExp(sReg, "gi"); 
    re.exec(sUrl); 
    return RegExp.$1; 
    }
  6. 地址跳转

    代码如下:

    var pn = $("#gotopagenum").val();//#gotopagenum是文本框的id属性 
    location.href = "NewList.aspx?pagenum="+pn;//location.href实现客户端页面的跳转

     

  7. 千分位

     代码如下:

    function Convert(money) 
    { 
    var s = money; //获取小数型数据 
    s += ""; 
    if (s.indexOf(".") == -1) s += ".00"; //如果没有小数点,在后面补个小数点和00 
    if (/\.\d$/.test(s)) s += "0"; //正则判断 
    while (/\d{4}(\.|,)/.test(s)) //符合条件则进行替换 
    s = s.replace(/(\d)(\d{3}(\.|,))/, "$1,$2"); //每隔3位添加一个 
    return s; 
    }

     

  8. 判断是否数字

     代码如下:
    function IsNumeric(txt) { 
    if (txt == "") { 
    return false; 
    } 
    
    if (txt.indexOf(",") > 0) { 
    txt = txt.replace(",", ""); 
    } 
    
    if (isNaN(txt)) { 
    return false; 
    } 
    else { 
    return true; 
    } 
    }

     

  9. 将数字进行两位小数的格式化

     代码如下:

    function changeTwoDecimal_f(x) { 
    var f_x = parseFloat(x); 
    if (isNaN(f_x)) { 
    alert('function:changeTwoDecimal->parameter error'); 
    return false; 
    } 
    f_x = Math.round(f_x * 100) / 100; 
    var s_x = f_x.toString(); 
    var pos_decimal = s_x.indexOf('.'); 
    if (pos_decimal < 0) { 
    pos_decimal = s_x.length; 
    s_x += '.'; 
    } 
    while (s_x.length <= pos_decimal + 2) { 
    s_x += '0'; 
    } 
    return s_x; 
    }

     

  10. Js 进行数字运算的函数 parseFloat parseInt

    js 当前日期 yyyy-mm-dd 预置查询条件

     代码如下:

    var now = new Date(); 
    var year = now.getYear(); 
    
    if (now.getYear() < 1900) { 
    year = now.getYear() + 1900; 
    } 
    
    var month = now.getMonth() + 1; 
    var day = now.getDate(); 
    
    if (month < 10) month = "0" + month; 
    if (day < 10) day = "0" + day; 
    
    $("#txtDate1").val(year.toString() + "-" + month.toString() + "-01"); 
    $("#txtDate2").val(year.toString() + "-" + month.toString() + "-" + day.toString());

    Js 获取时间戳,在某些情景下代替Guid

     代码如下:

    function NowTimeCode() 
    { 
    var Result=""; 
    
    var now = new Date(); 
    
    var year = now.getYear(); 
    
    if (now.getYear() < 1900) { 
    year = now.getYear() + 1900; 
    } 
    
    var month = now.getMonth() + 1; 
    var day = now.getDate(); 
    var hour = now.getHours(); 
    var minutes = now.getMinutes(); 
    var second = now.getSeconds(); 
    var millisecond = now.getMilliseconds(); 
    
    if (month < 10) month = "0" + month; 
    if (day < 10) day = "0"+ day; 
    if (hour < 10) hour = "0"+ hour; 
    if (minutes < 10) minutes = "0"+ minutes; 
    if (second < 10) second = "0"+ second; 
    
    if (millisecond < 10) 
    millisecond = "00"+ millisecond; 
    else 
    { 
    if (millisecond < 100) 
    { 
    millisecond = "0"+ millisecond; 
    } 
    } 
    
    Result = year.toString() + month.toString() + day.toString() + hour.toString() + minutes.toString() + second.toString() + millisecond.toString(); 
    
    return Result; 
    
    } 

     

     

posted @ 2016-10-24 17:51  zifieyu666  阅读(213)  评论(0编辑  收藏  举报