Q1:通过正则获得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中
<!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>
结果