JS自定义函数
前端设计  2023-09-28  3365次
  • JS自定义函数

//jq获取地址栏参数
//var id=getParam(''id'');
(function($){
$.getParam = function(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]);return null;
}
})(jQuery);

//获取地址栏参数
//var id=getQueryString(''id'');
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;
}

//批量替换,把f替换成e
String.prototype.ReplaceAll=function(f,e){
var reg = new RegExp(f,"g"); //创建正则RegExp对象
return this.replace(reg,e);
}

//写cookies
function setCookie(name,value){
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

//读取cookies
function getCookie(name){
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}

//删除cookies
function delCookie(name){
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null){
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
}

//批量提取网址
function TextURL(str){
var text = str;
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
text = text.replace(reg,"{$1$2}");
var arr=text.match(/\{(.+?)\}/g);
var txt=arr.join();
txt=txt.ReplaceAll("}","").ReplaceAll("{","");
return txt;
}

//批量提取网址
function doTextURL(str){
var text = str;
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
text = text.replace(reg,"{$1$2}|");
var arr = text.split("|");
var str = "";
for(var i=0;i<arr.length;i++){
var tmp = arr[i];
var sta = tmp.indexOf("{");
var end = tmp.indexOf("}");
str = str+tmp.substring(sta+1,end)+",";
}
str = str.ReplaceAll(",},",",").ReplaceAll(",,","");
return str;
}

//批量提取图片地址
function HtmlToImg(str){
var imgReg = /<img.*?(?:>|\/>)/gi;//匹配图片
var srcReg = /src=[\''\"]?([^\''\"]*)[\''\"]?/i;//匹配src属性
var arrA = str.match(imgReg);
//方法一
var srcA = "";
var arrB = [];
for(var i=0;i<arrA.length;i++){
var arrC = arrA[i].match(srcReg);
arrB.push(arrC[1]);
}
srcA = arrB.join();
//方法二
var srcB = "";
for(var i=0;i<arrA.length;i++){
var arrD = arrA[i].match(srcReg);
srcB = srcB + arrD[1] + ","
}
if(srcB.charAt(srcB.length-1)==","){
srcB = srcB.substring(0,srcB.length-1);
}
//return arrB;
//return {srcA, arrB};
return {
srcA:srcA,
arrB:arrB
};
}

//对象重新赋值
function objdome(){
var d = {type:0, txt:""};
d.type = 1;
d.txt = "这是重新赋值";
return d;
}

//对象测试
function objdome2(){
var str = "张三,李四,王麻子";
var arr = str.split(",");
var t = {type:1,msg:"消息",item:[{id:1,title:"这是标题",dtime:"2019-05-02"},{id:2,title:"这是标题2",dtime:"2019-06-21"},{id:3,title:"这是标题3",dtime:"2019-07-08"}]};
var d = {};
d.type = 1;
d.msg = "这是重新赋值";
return d;
//return arr;
}

//json对象
function ojson(){
//var t = {type:1,msg:"消息",item:[{id:1,title:"这是标题",dtime:"2019-05-02"},{id:2,title:"这是标题2",dtime:"2019-06-21"},{id:3,title:"这是标题3",dtime:"2019-07-08"}]};
var str = "本是后山人,偶做前堂客,醉舞经阁半卷书,坐井说天阔,大志戏功名,海斗量福祸,论到囊中羞涩时,怒指乾坤错";
var arr = str.split(",");
var d = {};
d.type = 1;
d.msg = "成功";
d.item = [];
/*
d.item[0] = {};
d.item[0].id = 10;
d.item[0].title = "这是标题";
d.item[0].dtime = "2019-05-02";
d.item[1] = {};
d.item[1].id = 20;
d.item[1].title = "这是标题2";
d.item[1].dtime = "2019-06-02";
*/
for (var i=0;i<arr.length;i++){
d.item[i] = {};
d.item[i].id = i+1;
d.item[i].title = arr[i];
d.item[i].dtime = "2019-05-02";
};
var p = {id:100,title:"这个是追加的",dtime:"2019-05-02"};
d.item.push(p); //追加数组
return d;
}

//json对象2
function ojson2(){
//动态添加属性
var obj = {}; //或者 var obj=new Object();
var key = "name";
var value = "张三"
obj[key] = value;
console.log(obj);

// 对象属性赋值
var a = {}; //定义对象
a.name = "李四"; //对象属性赋值
console.log(a);
}

WAP版 | 触屏版 | 电脑版

Copyright © 2014 shaooo.com