【问题标题】:How do i load page with default hash (ID) content?如何加载具有默认哈希 (ID) 内容的页面?
【发布时间】:2013-07-27 23:17:41
【问题描述】:

我有一个名为index.html 的页面,我想将其加载为index.html#home

因为我确实很少有具有不同 ID 的元素,并且我想默认加载 #home 内容。

所以当我们点击index.html页面时,它会重新加载到index.html#home(除非URL中没有指定哈希)

这怎么可能?并原谅我的英语。

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    你可以试试这个(检查MDN

    if(!window.location.hash) {
        location.hash = '#home';
    }
    

    DEMO.

    【讨论】:

    • 即使我尝试访问 index.html#contact,它也会转到 index.hrml#home 内容。通过添加 if 条件有什么解决方案?
    • 抱歉,没听懂您的提问。
    【解决方案2】:

    把它放在你的html页面的(不是head-见cmets)底部

    <script type='text/javascript'>
         document.getElementById("home").scrollIntoView();
    </script>
    

    【讨论】:

    • 您肯定必须等待 DOM 准备好才能工作吗?
    • 是的,我相信你是对的!当放置在 HTML 页面的底部而不是顶部时,这应该可以工作。
    • 这应该在页面加载后执行,正如here所说。
    【解决方案3】:

    如果您要在页面加载时滚动到 div,我建议您阅读(/应用)以下内容:

    jQuery: how to scroll to certain anchor/div on page load?

    【讨论】:

      猜你喜欢
      • 2011-03-31
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 2015-11-08
      • 2018-09-08
      • 1970-01-01
      • 2016-04-09
      • 2012-06-23
      相关资源
      最近更新 更多