【问题标题】:Remove hash sign and all the text after it in url删除 url 中的哈希符号及其后面的所有文本
【发布时间】:2016-10-10 18:46:12
【问题描述】:

如何删除 URL 的井号及其后面的文本?

例如,网址是http://www.website.com/home#content

我希望删除整个 #content 文本。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:
window.location.href.split('#')[0]  

你可以试试!

【讨论】:

  • 代码没有删除哈希。它只返回没有哈希的url。
  • history.pushState('', document.title, window.location.pathname);
【解决方案2】:

我认为这就是您要查找的内容...(即,当您单击其 href 中包含哈希的超链接时,您想去掉哈希并导航到剩余的 URL?)

<!DOCTYPE html>
<html>
    <body>
        <a id="someLink" href="/some_page#some_hash">Click Me</a>
        <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
        <script>
            $('#someLink').click(function (e) {
                // Prevent normal navigation to the href (the full URL with hash)
                e.preventDefault();
                // Navigate to "/some_page" (everything to left of the first "#")
                document.location = this.href.split('#')[0];
            });
        </script>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 2015-02-16
    • 1970-01-01
    • 2016-12-13
    • 2021-05-27
    • 2016-09-23
    • 1970-01-01
    • 2022-01-16
    • 2012-06-06
    相关资源
    最近更新 更多