需求:
a页面有多个参数
跳转到b页面
在b页面获取a页面带过来的参数
a.html
<!DOCTYPE HTML> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> <style> </style> </head> <body> <button type="button" class="a">跳转到b界面</button> </body> <script> //底部分类复选框取消和选中触发事件 $(".a").click(function() { window.location.href = "b.html?username=super&workNumber=M202105241431&type=2&beginTime=2021-05-24 14:31:00&endTime=2021-05-24 14:31:00"; }); </script> </html>
b.html
<!DOCTYPE HTML> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> <style> </style> </head> <body> </body> <script> //获取对方传过来的参数 function jqueryUrl(url) { debugger //将地址从"?"位置分割成两部分 var arr = url.split('?'); //取地址右边参数部分从"&"位置继续分割,成为单独参数列表 var params = arr[1].split('&'); //得到[a=1,b=2,c=3] //定义一个空对象 var obj = {}; for (var i = 0; i < params.length; i++) { var param = params[i].split('='); //得到[a,1]、[b,2]、[c,3] obj[param[0]] = param[1]; //为对象赋值 } return obj; } // url参数列表 var urlParams = jqueryUrl(location.href) console.log(urlParams) </script> </html>