Q1:經過正則得到url中的參數簡單實例html
url界面url
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <a href="script05.html?name=zhang&age=12&sex=男">點擊事件</a> </body> </html>
js中spa
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> //console.log(location.href); //console.log(location.search.substr(1)); //name=zhang&age=12&sex=男 /*經過正則獲取url中的參數*/ function getUrlParam(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null) return decodeURI(r[2]); return null; } var name = getUrlParam("sex"); console.log(name); </script> </body> </html>
結果code