//数组去重功能
var a = [1, 2, 3, 3, 5, 2, 3];
Array.prototype.distinct = function () {
var a = [];
var b = {};
for (var i = 0, length = this.length; i < length; i++) {
if (b[this[i]] !== 1) {
a.push(this[i]);
b[this[i]] = 1;
}
}
return a;
};
console.log(a.distinct());
//字符串去除前后空格 trim
String.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, '');
};
//获取url参数
String.prototype.getUrlParam = function(param){
//var url = location.search; //获取url中"?"符后的字串,包含问号
var params = {};
if (this.indexOf("?") != -1) {
var str = this.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
params[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
}
}
return params[param];
};
console.log(location.search.getUrlParam('id'));
js 常用小方法
最新推荐文章于 2015-10-06 08:29:02 发布