前端开发零碎知识记录

前端技术开发的汽车比赛小游戏
web前端开发技术聂常红
目前前端开发用到的技术

javascript 代码

//1:trim()去除string前后空格; str.replace(/[ ]/g,””) 去除string空格
var a = ‘ abcd efg hijkl ‘
console.log(‘去前后空格’, a.trim())
console.log(‘去空格 ‘, a.replace(/[ ]/g, ”))

javascript 代码

//2:数字转换成金额方式:
//链接:[js链接](https://www.cnblogs.com/mingmingruyuedlut/archive/2013/05/19/3082177.html)
var num = 123456.2346
num.toLocaleString() //123,456.235
console.log(num.toLocaleString())
//方法:用prototype方式将number数值转化为货币格式
Number.prototype.formatMoney = function(places, symbol, thousand, decimal) {
    places = !isNaN((places = Math.abs(places))) ? places : 2
    symbol = symbol !== undefined ? symbol : ‘$’
    thousand = thousand || ‘,’
    decimal = decimal || ‘.’
    var number = this,
        negative = number < 0 ? ‘-‘ : ”,
        i =
            parseInt((number = Math.abs(+number || 0).toFixed(places)), 10) +
            ”,
        j = (j = i.length) > 3 ? j % 3 : 0
    return (
        symbol +
        negative +
        (j ? i.substr(0, j) + thousand : ”) +
        i.substr(j).replace(/(\d{3})(?=\d)/g, ‘$1’ + thousand) +
        (places
            ? decimal +
             Math.abs(number – i)
                    .toFixed(places)
                    .slice(2)
            : ”)
    )
}
//例
var revenue = 12345678.1569
console.log(revenue.formatMoney()) // $12,345,678.00
console.log(revenue.formatMoney(0, ‘HK ‘)) // HK$ 12,345,678
//货币字符串的格式转化为number类型
var price = revenue.formatMoney(4, ‘HK$ ‘)
parseFloat(price.replace(/[^0-9-.]/g, ”)) // 12345.99
console.log(parseFloat(price.replace(/[^0-9-.]/g, ”)))
//方法:用function 函数方法将number数值转化为货币格式
function formatMoney(number, places, symbol, thousand, decimal) {
    number = number || 0
    places = !isNaN((places = Math.abs(places))) ? places : 2
    symbol = symbol !== undefined ? symbol : ‘$’
    thousand = thousand || ‘,’
    decimal = decimal || ‘.’
    var negative = number < 0 ? ‘-‘ : ”,
        i =
            parseInt((number = Math.abs(+number || 0).toFixed(places)), 10) +
            ”,
        j = (j = i.length) > 3 ? j % 3 : 0
    return (
        symbol +
        negative +
        (j ? i.substr(0, j) + thousand : ”) +
        i.substr(j).replace(/(\d{3})(?=\d)/g, ‘$1’ + thousand) +
        (places
            ? decimal +
             Math.abs(number – i)
                    .toFixed(places)
                    .slice(2)
            : ”)
    )
}
//例
console.log(formatMoney(12345, 2, ‘hk’)) // hk12,345.00

javascript 代码

//例如路径 blueberry.uat.byinfo.tech:70/console/html/login.html
console.log(window.location.protocol) //http:即,协议
console.log(window.location.host) //blueberry.uat.byinfo.tech:70设置或取得当前 URL 的主机名称和端口号;如果端口是默认的 80 端口而被省略,那么该属性取得的数据就不包含端口号 console.log(window.location.hostname);//即,设置或取得当前 URL 的主机名
console.log(window.location.hostname) //blueberry.uat.byinfo.tech设置或取得当前 URL 的主机名
console.log(window.location.protocol + ‘//’ + window.location.host)
web前端开发技术储久良答案
web前端技术开发
web前端开发技术的实践教学
» 本文来自:前端开发者 » 《前端开发零碎知识记录》
» 本文链接地址:https://www.rokub.com/7873.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!