jQuery中URL的最后一段 [英] Last segment of URL in jquery
本文介绍了jQuery中URL的最后一段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取网址的最后一段?我有以下脚本,该脚本显示了单击的锚标记的完整网址:
How do I get the last segment of a url? I have the following script which displays the full url of the anchor tag clicked:
$(".tag_name_goes_here").live('click', function(event)
{
event.preventDefault();
alert($(this).attr("href"));
});
如果网址是
http://mywebsite/folder/file
如何只在警报框中显示URL的文件"部分?
how do I only get it to display the "file" part of the url in the alert box?
推荐答案
您还可以使用 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.
这篇关于jQuery中URL的最后一段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文