javascript - jquery div根据url显示?

标签 javascript jquery

我有以下代码

<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>

网址是index.html。如果url像index.html#div1,我如何显示#div1;如果url像index.html#div2,我如何显示#div2

最佳答案

$(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();
    }
});

关于javascript - jquery div根据url显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832543/

相关文章:

javascript - 使用 jQuery 从下拉列表中获取选定的文本

jquery - 我的代码在 Safari 上不起作用

Jquery:将字符串转换为 bool 值

javascript - 如何添加延迟计时器?

javascript - 每次点击的总和

jquery - CSS 动画与 JQuery 动画

javascript - 配置 AngularJS 忽略旧版本的 jquery

javascript - 客户端 HTML MVC 渲染与通过 NodeJS 的服务器端渲染

javascript - 使用 JavaScript 的范围选择器

javascript - 日期以字符串格式出现在 json 对象中。我想要它的日期格式,因为我想显示它