Javascript jquery div是否根据url显示?

Javascript jquery div是否根据url显示?,javascript,jquery,Javascript,Jquery,我有以下代码 <html> <head> <script src="jquery.js"></script> <script> $(document).ready(function () { $("#div1").hide(); $("#div2").hide(); }); </script> </head>

我有以下代码

<html>
<head>
    <script src="jquery.js"></script>
    <script>
        $(document).ready(function () {
            $("#div1").hide();
            $("#div2").hide();
        });
    </script>

</head>
<body>
    <div id="div1">This is first Div</div>
    <div id="div2">Welcome At 2nd Div </div>
</body>
</html>

$(文档).ready(函数(){
$(“#div1”).hide();
$(“#div2”).hide();
});
这是第一组
欢迎来到第二分区

url是index.html。我如何显示#div1如果url像index.html#div1和#div2如果url像index.html#div2

人们,请再次阅读:“…我如何显示…”你确定你想要像
index.html#div1
这样的url吗?几乎不是最友好的书签URL。为什么不使用更友好的哈希值,然后将它们转换为代码中的特定div?很好的CSS选择器匹配-D@thecodeparadoxnp,虽然我不认为这是跨浏览器的,不是吗。散列一个chrome功能?我在工作,所以我不能真正测试它,以防它不是跨浏览器的,并且您需要它,请记住您可以使用
window.location.pathname
执行类似
window.location.pathname.match(/#div1/)的操作$(#div1.show():$(#div2.show())是,它工作正常,但代码1 location.hash不工作代码2工作正常还有一件事是hash总是返回一些内容例如,如果没有hash,它将返回完整的url,而不是返回false
$(document).ready(function){
    $("div[id^=div]").hide();
    var loc = window.location.href;
    if( loc.indexOf( '#' ) >= 0 ) {
        hash = loc.substr( loc.indexOf('#') + 1 ); // output: div1, div2 etc..
        $('#'+hash).show();
    }
});