🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
https://ihavenolimitations.xyz/book/a173512/qianduan/edit ## **Location 对象:** >[danger] 端口是默认80时不会显示 ``` console.log(window.location.href)// http://www.aoweiya.com/index/ProductOage/index.html?cate=74&p=2#dash console.log(window.location.pathname)// index/ProductOage/index.html console.log(window.location.hash)// #dash //返回一个URL的主机名和端口 console.log(window.location.host)// www.aoweiya.com //返回URL的主机名 console.log(window.location.hostname)// www.aoweiya.com console.log(window.location.port)// "" console.log(window.location.protocol)// http: console.log(window.location.search)// ?cate=74&p=2 ``` ``` (function ($) { String.prototype.joinUrl=function(url){ url=url??""; if (url=="" && this=="") { return ""; } else if(url=="" && this!=="") { return this+""; } else if(url!=="" && this=="") { return "?"+url; } else { return this+'&'+url; } } String.prototype.formatZero=function(num, len) { if (String(num).length > len) { return num; } return (Array(len).join(0) + num).slice(-len) } /* $.xx.xx({}) */ $.extend({ city:{ /* $.x.x({}) */ b:function(){ } }, /*$.x({})*/ urlGet:function(exclude){ //返回当前地址indow.location.search部分并排除指定参数 var url = document.location.href; //声明一个对象 var getRequest = new Object(); //获取?的位置 var index = url.indexOf("?") if(index != -1) { //截取出?后面的字符串 var str = url.substr(index + 1); //将截取出来的字符串按照&变成数组 strs = str.split("&"); //将get传参存入对象中 for(var i = 0; i < strs.length; i++) { getRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]); } if (exclude!==undefined ) { delete getRequest[exclude]; } if (Object.keys(getRequest).length>0) { var url=$.objToUrl(getRequest); } else { url=""; } return url; }else{ return ""; } }, getUrlParam:function(name) { //封装方法 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 var r = window.location.search.substr(1).match(reg); //匹配目标参数 if (r != null) return decodeURI(r[2]); return null; //返回参数值 }, objToUrl:function(url_obj){ const tempArray = []; for (const item in url_obj) { if (item) { tempArray.push(`${item}=${url_obj[item]}`); } } return `?${tempArray.join('&')}`; }, }); /* $(xxx).xxx */ $.fn.extend({ city:function(options){ $(".city-list").on("click", ".city-level-1>li>a", function(event){ $(this).toggleClass("on"); }); var cityparam=$.getUrlParam('city'); if (cityparam!==null) { var city=cityparam.split(','); city.map(function(e,i){ if (e=="") return; $(".city-list .city-level-1>li>a:contains("+e+")").addClass("on"); }) } }, getCity:function(mainCell,targetClass){ } }); })(jQuery); ```