获取路径的一部分

获取路径的一部分

本文介绍了jQuery-获取路径的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的网址: http://mysite.com/1/2/Simpson ,我想创建一个仅包含Simpson ...的变量.

I have a url that looks like this : http://mysite.com/1/2/Simpson and I want to create a variable that contains just Simpson...

不确定在此如何操作...

Not sure how to do it here...

我有:

var myvar = window.location.pathname

var myvar = window.location.pathname

并尝试使用子字符串进行某些操作,但似乎无法使其正常工作?

and have tried something using substring, but can't seem to get it to work?

推荐答案

这可能有效,但是您最好使用string window.location.href,而不要使用object window.location(其中包含许多不同的其中的对象):

this may work, but you might as well use the string window.location.href, not the object window.location (which contains many different objects within it):

var dirs = window.location.href.split( '/' );

但是,您也可以拆分一个window.location.pathname.它仅取决于您是否还希望第一个"/"前面的部分也位于数组中( http://mysite .com ).

however, there is a window.location.pathname that you could split, too. it just depends on if you want the part in front of the first "/" to be in the arrays as well (http://mysite.com).

var dirs = window.location.pathname.split( '/' );

这篇关于jQuery-获取路径的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:43