今天,当我偶然看到一个页面从另一个页面url获取参数时,我本可以想到使用split("?) )这一步一步分解所需的参数。 喜欢的朋友可以测试一下,希望对你有帮助!Js方法1:常规分析方法,指定参数名获取值。 function getquery string(name){ var reg = new RegExp(≥(^|&;amp)〉+姓名+>=([^&;amp]*)(& amp;amp|$)>,〉我:);var r = window . location . search . substr(1)。匹配(reg);if(r!= null){ return unescape(r[2]);}返回null}//用这种语气://http://orxhtml.github.io?a = 1 & ampampb = 2 & ampampc = 3 console . log(getquery string(" a "));console . log(getquery string(" b "));console . log(getquery string(" c "));截图:这里有一个例子:如果地址栏的网址是:abc.html?id = 123 & ampampURL = http://orxhtml.github.io然后,但是你用上面的方法调整用法:alert(getquery string(" URL ");会弹出一个对话框:内容为http://orxhtml.github.io如果使用:alert(getquery string(" id "));那么弹出的内容是123;当然,如果不传递参数,比如你的地址是abc.html,后面没有参数,你强制输出结果有时会报错:所以我们要加一个判断,判断我们请求的参数是否可以为空,先把值赋给一个变量:var myurl = getquery string(" URL ");如果(myurl!= null & ampamp& ampampmyurl.toString()。长度> 1){ alert(myurl);}js方法二:获取所有参数,这样就不会报错,返回的结果永远是一个对象!函数GetRequest(){ var URL = location . search;//获取“?”在url字符串var theRequest ={}运算符后;if(url.indexOf("?)!=-1){ var str = URL . substr(1);strs = str . split(& amp;amp”);for(var I = 0;i < strs.lengthi ++){theRequest[strs[i]。split(" = "[0]]= unescape(strs[I])。split(" = "[1]);} }返回请求;}//这样使用//http://orxhtml.github.io?a = 4 & ampampb = 5 & ampampc=6var请求= { };Request = GetRequest()。console.log(请求);console.log(请求[>一个:]);console.log(请求[>b > 1。]);console.log(请求[>c > 0。]);截图:本文内容属于个人原创作品,转载本文必须附上出处和参考链接。 关注,定时推送,互动精彩多了。如果有更好的见解,欢迎留言讨论!