Javascript jQuery如何从url中删除#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10036158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:35:25  来源:igfitidea点击:

jQuery how to remove # from url

javascriptjquery

提问by test

My url generates like this: only shows in ie9

我的网址生成如下:仅在 ie9 中显示

http://myurl.com/#/categories/posts

how do I remove # from url and make it like http://myurl.com/categories/postson rest of my pages? thanks.

如何从 url 中删除 # 并使其像http://myurl.com/categories/posts我的其他页面一样?谢谢。

can i use like this? how can I detect # cause front page has no #.

我可以这样使用吗?我如何检测 # 原因首页没有 #。

if ($.browser.msie  && parseInt($.browser.version, 10) === 9 && window.location.href.indexOf("#")) 
    {
        document.location.href = String( document.location.href ).replace( /#/, "" );
    }

to remove #/ used .replace( /#\//, "" );as mentioned Kevin B

删除 #/.replace( /#\//, "" );提到的凯文 B

回答by gotofritz

It's not really a JQuery job - use normal Javascript for this.

这不是真正的 JQuery 工作 - 为此使用普通的 Javascript。

document.location.href = String( document.location.href ).replace( /#/, "" );

EDITAdding @Kevin B's answer for completeness

编辑添加@Kevin B 的完整答案

document.location.href = String( document.location.href ).replace( "#/", "" );

回答by Karthikkumar

Just add 'return false' for anchor tag's click event.

只需为锚标记的点击事件添加“return false”。

$("#selectorname").click(function(){
    if ($.browser.msie  && parseInt($.browser.version, 10) === 9 && window.location.href.indexOf("#")) 
    {
        document.location.href = String( document.location.href ).replace( /#/, "" );
    }
    return false; /* This prevents url from # */
});

or else

要不然

<a href="#" onclick='return false'>Hey click me, I'm # free</a>

or just make it simple

或者简单点

$("#selectorname").click(function(e){
   /* some statements */
   e.preventDefault();
});

This prevents the default action not to be triggered. Detailed info here

这可以防止不触发默认操作。详细信息在这里

回答by Jones

$("a").attr("href",$("a").attr("href").replace(/#/, "")); 

回答by kavink

<a href="#" onclick="return false">Hey click me, I'm # free</a>does the work. I used this to hide # during Bootstrap dynamic tab switch. Thanks

<a href="#" onclick="return false">Hey click me, I'm # free</a>做这项工作。我用它来隐藏 # 在 Bootstrap 动态选项卡切换期间。谢谢

<ul class="nav nav-tabs">
                            <li class="active"><a data-toggle="tab" href="#i-ab" onclick="return false">ab</a></li>
                            <li><a data-toggle="tab" href="#i-cd" onclick="return false">cd</a></li>
                            <li><a data-toggle="tab" href="#i-ef" onclick="return false">ef</a></li>
                        </ul>