多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ <script> /* * queryURLParams:获取地址栏中问号传参的信息 * 'http://www.zhufengpeixun.cn/?lx=1&name=JS&from=baidu#video' 解析问号和井号后面的值,最后得到一个包含信息的对象 => { lx:1, name:'JS', from:'baidu', HASH:'video' } */ /* function queryURLParams(url) { // 1.创建A标签(A元素对象)来获取到问号参数和哈希值 let link = document.createElement('a'); link.href = url; let askText = link.search.substr(1), polText = link.hash.substr(1), obj = {}; // 2.向对象中进行存储 polText ? obj['HASH'] = polText : null; if (askText) { let arr = askText.split(/(?:&|=)/g); //=>同时按照两个字符来拆分:["lx", "1", "name", "JS", "from", "baidu"] for (let i = 0; i < arr.length; i += 2) { // console.log(arr[i], arr[i + 1]); 属性名和属性值 obj[arr[i]] = arr[i + 1]; } } return obj; } let result = queryURLParams('http://www.zhufengpeixun.cn/?lx=1&name=JS&from=baidu#video'); console.log(result); */ /* <!-- <a href="http://www.zhufengpeixun.cn/?lx=1&name=JS&from=baidu#video" id="link"> 在JS中获取的A元素对象,包含很多内置属性,存储对于HREF地址的解析: search:'?lx=1&name=JS&from=baidu', hash:'#video', host:'www.zhufengpeixun.cn', ... --> */ </script> <script> // 咱们正式课讲完正则,大家就会了 /* (proto => { function queryURLParams() { let obj = {}; this.replace(/([^?=&#]+)=([^?=&#]+)/g, (_, key, value) => obj[key] = value); this.replace(/#([^?=&#]+)/g, (_, hash) => obj['HASH'] = hash); return obj; } proto.queryURLParams = queryURLParams; })(String.prototype); console.log('http://www.zhufengpeixun.cn/?lx=1&name=JS&from=baidu#video'.queryURLParams()); */ </script> ~~~