简体   繁体   English

深度链接div块

[英]Deeplinking div blocks

I am looking for a way to deep link div blocks. 我正在寻找一种深层链接div块的方法。 On a particular page I might have several div blocks each with its own contents. 在特定页面上,我可能有几个div块,每个块都有自己的内容。 One of this blocks is visible, others are hidden. 此块之一是可见的,其他块是隐藏的。 Once a link or a button is pressed the corresponding div is shown and others are hidden. 一旦按下链接或按钮,就会显示相应的div,其他隐藏。 Here is the HTML for the divs: 这是div的HTML:

<div id="6ba28aae2dae153a1686cfee276632d8" class="page-block" style="display: block;">
    <p>
    1st block. 
    </p>        
</div>
<div id="55cead0effa915778913d8667d0ae3a9" class="page-block" style="display: none;">
    <p>
    2nd block. 
    </p>
</div>

And here is the JavaScript used to switch the blocks. 这是用于切换块的JavaScript。

/* Hide and show necessary blocks. */
function switchBlocks(UID)
{
    var blocks = $('div.page-block');

    for (i=0; i<blocks.length; i++) 
    {
        if (blocks[i].id == UID) 
        {
            blocks[i].style.display= 'block';

            // Get the current URL and split it at the # mark
            urlArray = window.location.href.split("#");
            // Select the part before #
            subURL = urlArray[0];
            // Create a fake URL by adding UID to subURL
            history.pushState(null, null, subURL + '#' + UID);
        } 
        else 
        {
            blocks[i].style.display= 'none';
        }
    }
}

What I am trying to do now is assign each block its unique URL by using the blocks ID. 我现在想要做的是通过使用块ID为每个块分配其唯一的URL。 I am able to update the URL with the relevant ID yet can't figure out how to link the particular URL to a specific block so that the corresponding block is shown when accessing its URL. 我能够使用相关ID更新URL,但无法弄清楚如何将特定URL链接到特定块,以便在访问其URL时显示相应的块。

I have studying the tutorial on the HTML5 History API but can't quite figure out how to apply it to my case. 我已经研究了有关HTML5历史记录API的教程,但还不太清楚如何将其应用于我的案例。

if(window.location.hash == "#55cead0effa915778913d8667d0ae3a9")
{
 $("div.page-block").hide();
 $("div#55cead0effa915778913d8667d0ae3a9").show();
}

or 要么

hash_id = window.location.hash;
if(hash_id.length > 0)
{
 $("div.page-block").hide();
 $(hash_id).show();
}

I think it is better to add and remove classes then having the desired css effect. 我认为最好先添加和删除类,然后再具有所需的CSS效果。 I believe this is more efficient but I could be wrong. 我相信这样做会更有效率,但我可能是错的。

You could also store a reference to the old div that is being shown so that you could just hide that one and show the other rather then iterating over an entire list of divs. 您还可以存储对正在显示的旧div的引用,以便您可以隐藏一个并显示另一个,而不是遍历整个div列表。 In reality you only need to show one div and hide one div. 实际上,您只需要显示一个div并隐藏一个div。

As far as browser history support. 就浏览器历史记录支持而言。 Do you need this to be cross browser supported? 需要跨浏览器支持吗? html5 history API is not supported in all browsers. 并非所有浏览器都支持html5历史记录API。 This may not be a problem or concern for you. 对您来说,这可能不是问题或担忧。

List of supported browsers 支持的浏览器列表
http://caniuse.com/#search=history http://caniuse.com/#search=history

I have used jquery bbq to add history support to a web application. 我使用了jQuery烧烤来向Web应用程序添加历史记录支持。 It works well in older browsers. 它在较旧的浏览器中效果很好。
http://benalman.com/projects/jquery-bbq-plugin/ http://benalman.com/projects/jquery-bbq-plugin/

For big javascript projects Backbone.js is the way to go it has browser history support as well as many other helpful built in functions that make it easy to manage your code base. 对于大型javascript项目,Backbone.js就是其中的一种方式,它具有浏览器历史记录支持以及许多其他有用的内置函数,可轻松管理您的代码库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM