自己简单的模拟讲解了一下如何学习并且模拟url地址中获取传递参数的属性,及其属性值。
首先模拟两个页面的跳转:1.html跳转到2.html并且跳转过程中,利用a标签传参。
1.html中代码正文部分如下:
<a href="map2.html?userName=zhanghao&userId=123">跳转到2页面</a>2.heml中代码正文部分如下:
<body> <div> 这是map2页面 </div> </body> </html><script> //假设自己跳转到该页面的时候 地址栏的内容为:http://localhost:63342/%E5%9C%B0%E5%9B%BEapi/map2.html?userName=zhanghao&userId=123 //下边就是自己写的代码 function getRequest1(){ var str=location.search; // ?userName=zhanghao$userId=123 if(str){ var url=str.substr(1),arr=url.split('&'),len=arr.length,i= 0,request={}; // ["userName=zhanghao", "userId=123"] for(;i<len;i++){ request[arr[i].split('=')[0]]=arr[i].split('=')[1]; // ["userName", "zhanghao"] ["userId", "123"] } return request; //{userName: "zhanghao", userId: "123"} }else{ alert('没有传递参数'); } } var Request=getRequest1();//可以查看返回的对象,根据对象的参数获取其中的键值对 var name=Request.userName;//获取某个属性的值 console.log(name); </script>