JS篇

作者: hey_前端豆 | 来源:发表于2017-06-26 13:53 被阅读0次
    • 时间戳转换为年月日核心代码
    /*时间戳,以ms为单位*/
    function changeTime (inputTime){
        var date = new Date(inputTime);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }
    
    • input标签只输入数字
     <span>
        <input class="idCard" type="text" name="idCard" placeholder="身份证"   onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"></span>
    
    • JS运算
      浮点数运算会有计算bug
      精确两位用toFixed(2)
    • 数组的深拷贝复杂对象集合
    var deepCopy = function(o) {
        if (o instanceof Array) {
            var n = [];
            for (var i = 0; i < o.length; ++i) {
                n[i] = deepCopy(o[i]);
            }
            return n;
    
        } else if (o instanceof Object) {
            var n = {}
            for (var i in o) {
                n[i] = deepCopy(o[i]);
            }
            return n;
        } else {
            return o;
        }
    }
    
    • url跳转页面时带参为中文,格式化
    function(){
            let info = JSON.stringify(item);
            let recode = encodeURIComponent(info);
            console.log(recode);
            window.location.href="health-evaluation-scheme-pic.html?item="+recode
    }
    
    • 字符串截取,中文
    function getQueryString(key){
        var reg = new RegExp("(^|&)"+key+"=([^&]*)(&|$)");
        var result = window.location.search.substr(1).match(reg);
        return result?decodeURIComponent(result[2]):null;
    }
    
    • 字符串截取,非中文
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    

    相关文章

      网友评论

          本文标题:JS篇

          本文链接:https://www.haomeiwen.com/subject/dkltqxtx.html