【问题标题】:jQuery URL split and grabjQuery URL 拆分和抓取
【发布时间】:2012-03-18 12:03:05
【问题描述】:

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

有没有在#!/之后获取url和吐出部分?我想要编辑和 ID。

【问题讨论】:

  • 您需要php或JavaScript`的解决方案吗?

标签: jquery url split


【解决方案1】:
var edit = window.location.hash.split('/')[1],
      ID = window.location.hash.split('/')[2];

【讨论】:

    【解决方案2】:

    您可以使用此代码

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

    如果你只是想要编辑后的数字,你也可以使用正则表达式

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

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

    【讨论】:

    • 谢谢!拆分网址对我有很大帮助
    猜你喜欢
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多