//超出指定长度后,使用...替换
export const formatEllipsis = (str = "", limitLen = 24) => {
    if (str.length >= limitLen) {
        str = str.slice(0, limitLen) + "...";
    }
    return str;
}
//获取url中指定key的参数值
export const getQueryParam = (name, url = null) => {
    if (!url) url = window.location.search
    var q = url.match(new RegExp('[?&]' + name + '=([^&#]*)'))
    return (q && q[1]) || ''
}
//修改url中指定key的参数值
export const setQueryParam = (key, value, uri = null) => {
    uri = uri || window.location.href;//window.location.search.href
    var re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i')
    var separator = uri.indexOf('?') !== -1 ? '&' : '?'
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + '=' + value + '$2')
    } else {
        return uri + separator + key + '=' + value
    }
}

function pad0(n) {
    return n < 10 ? '0' + n : n
}
//格式化数字,四舍五入(数字/保留小数位/分隔符/前缀)
//例: formatNumber(12300094.125,2,',','$') = $12,300,094.13
export const formatNumber = (num, precision = 2, separator = ',', prefix = '') => {
    let parts;
    // 判断是否为数字
    if (!isNaN(parseFloat(num)) && isFinite(num)) {
        // 把类似 .5, 5. 之类的数据转化成0.5, 5
        num = Number(num)
        // 处理小数点位数
        num = (typeof precision !== 'undefined'
            ? num.toFixed(precision)
            : num
        ).toString()
        // 分离数字的小数部分和整数部分
        parts = num.split('.')
        // 整数部分加[separator]分隔, 借用一个著名的正则表达式
        parts[0] = parts[0]
            .toString()
            .replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + (separator || ','))

        return prefix + parts.join('.')
    }
    return ''
}
//获取日期 yyyy-MM 是否包含中文 cn:0/1
export const formatMonth = (val, cn) => {
    if (!val) return ''
    const date = new Date(val)
    return (
        date.getFullYear() +
        (cn ? '年-' : '-') +
        pad0(date.getMonth() + 1) +
        (cn ? '月' : '')
    )
}
//获取日期 yyyy-MM-dd 是否包含中文 cn:0/1
export const formatDate = (val, cn) => {
    if (!val) return ''
    const date = new Date(val)
    return (
        date.getFullYear() +
        (cn ? '年-' : '-') +
        pad0(date.getMonth() + 1) +
        (cn ? '月-' : '-') +
        pad0(date.getDate()) +
        (cn ? '日' : '')
    )
}
//获取日期 yyyy-MM-dd HH:mm 是否包含中文 cn:0/1
export const formatDateTime = (val, cn) => {
    if (!val) return ''
    const date = new Date(val)
    return (
        formatDate(val, cn) +
        ' ' +
        pad0(date.getHours()) +
        ':' +
        pad0(date.getMinutes())
    )
}
//添加百分号
export const formatRate = (val) => {
    if (!val) return '0%'
    return val + '%'
}
//保留两位小数,无四舍五入,无小数补 .00
export const formatPrice = (val) => {
    if (!val) return '0.00'
    val = val.toFixed(2) + ''
    if (val.indexOf('.') == -1) {
        return val + '.00'
    } else {
        return val
    }
}
//保留N位小数,四舍五入,无小数补 .00
export const formatPriceUp = (val, n) => {
    let res = (Math.round(val * 100) / 100).toFixed(n);
    return res;
}

  这是用在vue.js中的,所以加了导出 export

--获取后缀名,结果 .jpg

var extName = "/upload/head_img/20150902102539.jpg";
var ta = extName.substring(extName.indexOf("."));

 

--判断是否是焦点

if (document.activeElement.id == "txtAutoC") {
//判断是否是焦点
}

 

http://www.cnblogs.com/yejiurui/p/3413796.html

http://blog.csdn.net/xyxjn/article/details/41805365

 

//超出指定长度后,使用...替换
export const formatEllipsis = (str = "", limitLen = 24) => {
    if (str.length >= limitLen) {
        str = str.slice(0, limitLen) + "...";
    }
    return str;
}
//获取url中指定key的参数值
export const getQueryParam = (name, url = null) => {
    if (!url) url = window.location.search
    var q = url.match(new RegExp('[?&]' + name + '=([^&#]*)'))
    return (q && q[1]) || ''
}
//修改url中指定key的参数值
export const setQueryParam = (key, value, uri = null) => {
    uri = uri || window.location.href;//window.location.search.href
    var re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i')
    var separator = uri.indexOf('?') !== -1 ? '&' : '?'
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + '=' + value + '$2')
    } else {
        return uri + separator + key + '=' + value
    }
}

function pad0(n) {
    return n < 10 ? '0' + n : n
}
//格式化数字,四舍五入(数字/保留小数位/分隔符/前缀)
//例: formatNumber(12300094.125,2,',','$') = $12,300,094.13
export const formatNumber = (num, precision = 2, separator = ',', prefix = '') => {
    let parts;
    // 判断是否为数字
    if (!isNaN(parseFloat(num)) && isFinite(num)) {
        // 把类似 .5, 5. 之类的数据转化成0.5, 5
        num = Number(num)
        // 处理小数点位数
        num = (typeof precision !== 'undefined'
            ? num.toFixed(precision)
            : num
        ).toString()
        // 分离数字的小数部分和整数部分
        parts = num.split('.')
        // 整数部分加[separator]分隔, 借用一个著名的正则表达式
        parts[0] = parts[0]
            .toString()
            .replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + (separator || ','))

        return prefix + parts.join('.')
    }
    return ''
}
//获取日期 yyyy-MM 是否包含中文 cn:0/1
export const formatMonth = (val, cn) => {
    if (!val) return ''
    const date = new Date(val)
    return (
        date.getFullYear() +
        (cn ? '年-' : '-') +
        pad0(date.getMonth() + 1) +
        (cn ? '月' : '')
    )
}
//获取日期 yyyy-MM-dd 是否包含中文 cn:0/1
export const formatDate = (val, cn) => {
    if (!val) return ''
    const date = new Date(val)
    return (
        date.getFullYear() +
        (cn ? '年-' : '-') +
        pad0(date.getMonth() + 1) +
        (cn ? '月-' : '-') +
        pad0(date.getDate()) +
        (cn ? '日' : '')
    )
}
//获取日期 yyyy-MM-dd HH:mm 是否包含中文 cn:0/1
export const formatDateTime = (val, cn) => {
    if (!val) return ''
    const date = new Date(val)
    return (
        formatDate(val, cn) +
        ' ' +
        pad0(date.getHours()) +
        ':' +
        pad0(date.getMinutes())
    )
}
//添加百分号
export const formatRate = (val) => {
    if (!val) return '0%'
    return val + '%'
}
//保留两位小数,无四舍五入,无小数补 .00
export const formatPrice = (val) => {
    if (!val) return '0.00'
    val = val.toFixed(2) + ''
    if (val.indexOf('.') == -1) {
        return val + '.00'
    } else {
        return val
    }
}
//保留N位小数,四舍五入,无小数补 .00
export const formatPriceUp = (val, n) => {
    let res = (Math.round(val * 100) / 100).toFixed(n);
    return res;
}
posted on 2015-08-25 10:33  邢帅杰  阅读(163)  评论(0编辑  收藏  举报