且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery中URL的最后一段

更新时间:2023-02-20 08:45:56

您还可以使用 substring()函数从该位置开始返回子字符串:

You can also use the lastIndexOf() function to locate the last occurrence of the / character in your URL, then the substring() function to return the substring starting from that location:

console.log(this.href.substring(this.href.lastIndexOf('/') + 1));

那样,您将避免像split()那样创建包含所有URL段的数组.

That way, you'll avoid creating an array containing all your URL segments, as split() does.