zoukankan      html  css  js  c++  java
  • javascript一些好用的方法

    1、字符串操作

    String.prototype.stringFormat = function() {
        var str = this;
        for (var i = 0; i <= arguments.length; i++) {
            str = str.replace(new RegExp("\{" + (i) + "\}", "g"), arguments[i] || "");
        }
        return str;
    }
    
    var str = "{0}--{1}";
    alert(str.stringFormat('hello','world'))
    String.prototype.startWith=function(str){    
      var reg=new RegExp("^"+str);    
      return reg.test(this);       
    } 
    
    String.prototype.endWith=function(str){    
      var reg=new RegExp(str+"$");    
      return reg.test(this);       
    }

    2、日期格式化

    Date.prototype.Format = function(fmt){  
      var o = {   
        "M+" : this.getMonth()+1,                 //月份   
        "d+" : this.getDate(),                    //
        "h+" : this.getHours(),                   //小时   
        "m+" : this.getMinutes(),                 //
        "s+" : this.getSeconds(),                 //
        "q+" : Math.floor((this.getMonth()+3)/3), //季度   
        "S"  : this.getMilliseconds()             //毫秒   
      };   
      if(/(y+)/.test(fmt))   
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
      for(var k in o)   
        if(new RegExp("("+ k +")").test(fmt))   
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
      return fmt;   
    } 
    alert(new Date().Format('yyyy-MM-dd'))

    3、操作Cookie

    /**
     * c_name:key  
     * value:
     * expireHour:时间长度
     */
    function setCookie(c_name, value, expireDate){
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expireDate);
        document.cookie = c_name+ "="+ escape(value)+ ((expireDate == null) ? "" : ";expires="
            + exdate.toGMTString())+";path=/";
    }
        
    /**
     * 得到cookie,根据key得到相应的value
     */
    function getCookie(c_name){
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=")
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1
                c_end = document.cookie.indexOf(";", c_start)
                if (c_end == -1)
                    c_end = document.cookie.length
                return unescape(document.cookie.substring(c_start, c_end))
            }
        }
        return "";
    }
        
        
    /**
     * 删除指定名称的cookie(将指定的cookie时间设置为过期)
     */
    function delCookie(name){
        var date =new Date();
        date.setTime(date.getTime()-10000);
        document.cookie=name+"=a;expires="+date.toGMTString();
    }
  • 相关阅读:
    函数waitpid和WTERMSIG说明(转)
    WIFEXITED WEXITSTATUS WIFSIGNALED(转)
    有关于malloc申请内存和free内存释放
    Using 1-Wire device with Intel Galileo
    Intel Galileo驱动单总线设备(DHT11DHT22)(转)
    360度舵机和180度舵机控制方法小结(转)
    warning: the `gets' function is dangerous and should not be used.(转)
    C语言fgetpos()函数:获得当前文件的读写指针(转)
    关于arduino清空串口缓存(转)
    修改Arduino串口缓冲区大小(转)
  • 原文地址:https://www.cnblogs.com/sunxueqiang0329/p/4349600.html
Copyright © 2011-2022 走看看