jQuery URL拆分和抓取

Rus*_*wer 5 url jquery split

所以我有一个URL,我知道如何从URL获取$ _GET,但我的URL是http://www.example.com/#!/edit/2695.

有没有拿到网址并随后吐出部分#!/?我想要编辑和ID.

jfr*_*d00 11

您可以使用此代码

var url = "http://www.mysite.com/#!/edit/2695";
var pieces = url.split("/#!/");
pieces = pieces[1].split("/");

// pieces[0] == "edit"
// pieces[1] == "2695"
Run Code Online (Sandbox Code Playgroud)

如果您只想在编辑后输入数字,也可以使用正则表达式

var url = "http://www.mysite.com/#!/edit/2695";
var match = url.match(/#!\/edit\/(\d+)/);
if (match) {
    // match[1] == "2695"
}
Run Code Online (Sandbox Code Playgroud)

你可以看到他们都在这里工作:http://jsfiddle.net/jfriend00/4BTyH/


ade*_*neo 6

var edit = window.location.hash.split('/')[1],
      ID = window.location.hash.split('/')[2];
Run Code Online (Sandbox Code Playgroud)