【问题标题】:Get the Value after `/` from Variable从变量中获取`/`之后的值
【发布时间】:2017-11-28 00:27:10
【问题描述】:

我试图使用 javascript 获取当前页面 pathname。我能够得到它,但我用 Forward Slash / 得到它。我想从变量中删除 /

这是我尝试过的代码。 :)

var URLPath = window.location.pathname;
alert(URLPath);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【问题讨论】:

    标签: javascript url pathname


    【解决方案1】:

    使用slicesubstr 获取从第二个字符开始的子字符串:

    var URLPath = window.location.pathname.slice(1);
    //                                    ^^^^^^^^^
    alert(URLPath);

    【讨论】:

      【解决方案2】:

      var URLPath = window.location.pathname;
      alert(URLPath.substr(1));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 2015-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多