【问题标题】:Hash to hide divs - direct access to page URL [closed]哈希隐藏 div - 直接访问页面 URL [关闭]
【发布时间】:2013-10-03 00:48:34
【问题描述】:

我正在开发一个网站,但遇到了以下问题。

我的网站由 4 个页面组成,所有这些页面都是 css div,它们会根据页面上的菜单栏隐藏和显示。

这是我的例子:http://jsfiddle.net/DcwUu/

HTML:

<button class="home-button">Home</button>
<button class="download-button">Download</button>
<button class="about-button">About</button>
<button class="contact-button">Contact</button>

<div class="home-container">
    <div class="left-corner"></div>
    This is the home page!
</div>

<div class="download-container">
    <div class="left-corner"></div>
    This is the download page!
</div>
<div class="about-container">
    <div class="left-corner"></div>
    This is the about page!
</div>
<div class="contact-container">
    <div class="left-corner"></div>
    This is the contact page!
</div>

jQuery:

$('.home-button').click(
    function(){
        $('.home-container').show();
        $('.download-container').hide();
        $('.about-container').hide();
        $('.contact-container').hide();
    }
);
$('.download-button').click(
    function(){
        $('.download-container').show();
        $('.about-container').hide();
        $('.contact-container').hide();
        $('.home-container').hide();
    }
);
$('.about-button').click(
    function(){
        $('.about-container').show();
        $('.contact-container').hide();
        $('.download-container').hide();
        $('.home-container').hide();        
    }
);
$('.contact-button').click(
    function(){
        $('.contact-container').show();
        $('.home-container').hide();       
        $('.download-container').hide();
        $('.about-container').hide();
    }
);

CSS:

.download-container {display:none;}
.about-container {display:none;}
.contact-container {display:none;}
.home {display:block;}

我的主页是 index.php,我可以通过 localhost/mysite/index.php 访问它

当我点击任何链接并显示/隐藏 div 时,我的 URL 更改为 localhost/mysite/index.php#

我似乎找不到通过 URL 直接访问 4 个“页面”中的任何一个的方法。

谢谢!

【问题讨论】:

  • 您可以发布您的代码或相关示例吗?
  • 我能够使用 Louis 的答案以及 Brian 的答案来解决这个问题。
  • Wesley - 我已将代码添加到页面中。

标签: jquery css url html hash


【解决方案1】:

穷人的做法是这样的:

var hash = window.location.hash.slice(1);
$('.page').hide();
$('#' + hash).show();

所以,给定这样的页面,

<div class="page" id="main"></div>
<div class="page" id="foo"></div>
<div class="page" id="bar"></div>
<div class="page" id="baz"></div>

如果用户转到localhost/mysite/#foo,则foo 页面将可见。

【讨论】:

  • 谢谢!我喜欢 .slice 删除 # 符号。这是一个很难问的问题,因为我真的不知道自己在寻找什么。
【解决方案2】:

要直接访问,只需在 URL 中找到哈希:

var hash = window.location.hash
$(hash).show();

根据这个值,显示正确的 div。

对于加载页面上的变化,监听散列的变化; jQuery 为您提供了一个漂亮的hashchanged 事件:

$(window).on('hashchange', function() {
  .. work ..
});

在此事件中,提取您的哈希值,该哈希值应存储在 window.location.hash 中。根据它的值,显示/隐藏相应的 div 元素。

看这个简单的例子:http://jsfiddle.net/aUsHh/3/

【讨论】:

  • 谢谢,这正是我不明白的。以后我会尽量详细说明我的问题。
  • 这正是我正在寻找的,我也不知道如何表达它。幸运的是,谷歌搜索让我进入了这个页面。我认为它应该重新开放。
猜你喜欢
  • 2015-05-02
  • 2013-05-14
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 2011-08-26
  • 1970-01-01
  • 2013-10-11
相关资源
最近更新 更多