如何使用jQuery获取URL的最后一个参数.例如,在以下URL中,我想获得1.
localhost/x/y/page/1/
Run Code Online (Sandbox Code Playgroud)
我正在努力追随
var url = $(location).attr('href');
var parts = url.split("/");
var last_part = parts[parts.length-1];
alert(last_part);
Run Code Online (Sandbox Code Playgroud)
它返回空值.
rel*_*lic 17
如果"/"肯定在你的网址的末尾,无论如何..然后只需更改:
var last_part = parts[parts.length-2]
Run Code Online (Sandbox Code Playgroud)
(因为ncdreamy的评论,使用2而不是1).
此外,您不需要var var var var var ...只需一次var和一个逗号分隔符:
var url = $(location).attr('href'),
parts = url.split("/"),
last_part = parts[parts.length-2];
Run Code Online (Sandbox Code Playgroud)
您可以使用:
hrefurl=$(location).attr("href");
last_part=hrefurl.substr(hrefurl.lastIndexOf('/') + 1)
Run Code Online (Sandbox Code Playgroud)
使用jquery
$(location).attr("href").split('/').pop();
Run Code Online (Sandbox Code Playgroud)
非常简单的解决方案
let url = $(location).attr('href');
let urlKey = url.replace(/\/\s*$/, "").split('/').pop();
console.log(urlKey)
Run Code Online (Sandbox Code Playgroud)
/url 中的最后一个/并弹出最后一个元素| 归档时间: |
|
| 查看次数: |
18517 次 |
| 最近记录: |