如何使用 jQuery 获取路径的一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6609996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:07:03  来源:igfitidea点击:

How to get part of the path with jQuery?

jquery

提问by Iladarsda

I have following URL www.webiste.com/Services/allservices.html

我有以下网址 www.webiste.com/Services/allservices.html

How could I get part of this URL with jQuery?

如何使用 jQuery 获取此 URL 的一部分?

e.g 'services'or 'allservices.html'

例如'services'或'allservices.html'

Any suggestion much appreciated.

任何建议非常感谢。

回答by Sotiris

var url = "www.webiste.com/Services/allservices.html"
    url = url.split("/");
    alert(url[1]);

In the above example split the string urlwhen find /(separator) and save it in an array. Then using the proper index, you can use the substring you wish (in the above example will alert Services).

在上面的示例中,url在查找/(分隔符)时拆分字符串并将其保存在数组中。然后使用正确的索引,您可以使用您想要的子字符串(在上面的示例中将使用 alert Services)。

Demo: http://jsfiddle.net/jcNSs/

演示:http: //jsfiddle.net/jcNSs/

More info for split(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

更多信息split()https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split

回答by Simeon

var url = 'http://www.website.com/Services/allservices.html';

// Getting the file name (allservices.html)
var fn = url.split('/').reverse()[0];

// Getting the extension (html)
var ext = url.split('/').reverse()[0].split('.').reverse()[0];

// Getting the last middle part (services)
var lm = url.split('/').reverse()[1];

回答by StuperUser

If you are using the JavaScript window.locationyou can split location.pathnameusing '/' and use pop()to get the last element or get by index:

如果您使用的是 JavaScript window.location,则可以location.pathname使用 '/'进行拆分并用于pop()获取最后一个元素或按索引获取:

var splitUrlArray = urlString.split('/'); 
var lastPart = splitUrlArray.pop();
var firstPart = splitUrlArray[0]; 

Read http://davidwalsh.name/javascript-window-locationfor more details on the window.location.

阅读http://davidwalsh.name/javascript-window-location了解更多关于window.location.

回答by Andy Hunt

Split it by "/" and then choose the last element.

用“/”分割,然后选择最后一个元素。

var parts = $("selectorForText").val().split("/");
//parts[0] = www.website.com
//parts[1] = Services
//parts[2] = allservices.html

Something to that effect will work - the important bits being the split()function and the recognition that it returns an array of strings

达到这种效果的东西会起作用 - 重要的部分是split()函数和它返回一个字符串数组的识别

--Edited to provide example--

--编辑以提供示例--

回答by Matt Powell

This isn't a jQuery problem as there are a couple of javascript functions that can help you (obviously it'll still work in and around jQuery). The easiest way is to just split on the path separator and then manipulate it however you like:

这不是 jQuery 问题,因为有几个 javascript 函数可以帮助您(显然它仍然可以在 jQuery 中和周围工作)。最简单的方法是在路径分隔符上拆分,然后随意操作它:

a = "www.website.com/Services/allservices.html".split("/")

 => ["www.website.com", "Services", "allservices.html"]

Then you can piece it together however you like:

然后你可以随意拼凑:

  • Host

    a.slice(0) => "www.website.com"

  • Path

    a.slice(1, a.size()) => ["Services", "allservices.html"] a.slice(1, a.size()).join("/") => "Services/allservices.html"

  • 主持人

    a.slice(0) => "www.website.com"

  • 小路

    a.slice(1, a.size()) => ["Services", "allservices.html"] a.slice(1, a.size()).join("/") => "Services/allservices .html”

etc.

等等。

Enjoy :-)

享受 :-)

回答by Iladarsda

Another simpler way:

另一种更简单的方法:

urlParts = "www.website.com/About/subpage1.html".split("/")
    var Part1= urlParts[1]+"/";   // e.g. About/
    var Part2 = urlParts[2];      // e.g. subpage1.html