分享几个JS常用方法封装

获取URL查询参数

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;

}

获取Cookie

function GetCookie(name){

    for(var i=0;i

        if(document.cookie.split(';')[i].split('=')[0].trim()==name)

        {

            return document.cookie.split(';')[i].split('=')[1];

        }

    }

}

设置随机颜色

function setColor(){

return Math.random().toString(16).slice(-6);

}

function setColor() {

    var r = Math.floor(Math.random()*256);

    var g = Math.floor(Math.random()*256);

    var b = Math.floor(Math.random()*256);

    var a = Math.floor(Math.random()*10+0)/10;

    var rgba = '('+r+','+g+','+b+',' + a + ')';

    return rgba;

}

你可能感兴趣的:(JavaScript)