0

如果我有一个index.html文件并且它包含多个页面。当我加载页面时,我希望 URL 包含 hash #first,也就是index.html#first. 相反,URL 显示index.html.

到目前为止,我的工作是创建一个虚假的首页,然后直接链接到index.html#first,希望永远不会显示“真实的”首页,即index.html#fake.

有没有更好的方法来做到这一点?

<body>

    <!--
        fake page
    -->
    <div data-role="page" id="fake">
        <div data-role="content">
            fake
        </div>
    </div>

    <!--
        first
    -->
    <div data-role="page" id="first">
        <div data-role="content">
            first
        </div>
    </div>

    <!--
        second
    -->
    <div data-role="page" id="second">
        <div data-role="content">
            second
        </div>
    </div>
</body>
4

2 回答 2

1

如果 index.html 页面上存在哈希码,为什么不签入 JS,如果没有,那么您可以将浏览器重定向到index.html#first

$(function () {
    if (window.location.hash.length == 0) {
        window.location = window.location.href + '#first';    
    }
});
于 2012-03-23T16:08:53.810 回答
0

配置您的网络服务器,以便对“index.html”请求和对“/”的请求(没有文件的路径!)返回301 Moved Permanently重定向到“index.html#first”的响应

也就是说,我无法想象你为什么要将哈希强制到 url 上;也许是一个不够强大的库?正确编码的http://example.com/,http://example.com/index.htmlhttp://example.com/index.html#firstcan and should (恕我直言) 都表现相同。

于 2012-03-23T16:21:45.717 回答