简体   繁体   English

如何使用#创建动态页面?

[英]How to create dynamic pages using # ?

I have noticed that aa lot of pages like Twitter and a few other sites have incorporated AJAX into their design. 我注意到很多像Twitter和其他一些网站的网页都将AJAX融入他们的设计中。 One thing that has caught my attention is the use of #! 引起我注意的一件事是使用#! in URLs. 在URL中。 I am wonerding how I can do this for myself or the method they are using, Thanks! 我想知道如何为自己或他们使用的方法做到这一点,谢谢!

You can start with something very simple and use either Hashchange or BBQ plugin. 你可以从非常简单的东西开始,使用HashchangeBBQ插件。 Read the manuals of both and you will grasp the idea. 阅读两者的手册,你将掌握这个想法。

And here is a short and general introduction: http://code.google.com/intl/en-EN/web/ajaxcrawling/docs/html-snapshot.html 这里有一个简短而通用的介绍: http//code.google.com/intl/en-EN/web/ajaxcrawling/docs/html-snapshot.html

UPDATE: 更新:

Well, let's take Hashchange plugin as an example. 好吧,我们以Hashchange插件为例。 The following code is very primitive, but I think it will help to understand the basic part 以下代码非常原始,但我认为这将有助于理解基本部分

HTML: HTML:

<ul>
    <li><a href="/about">About</a></li>
    <li><a href="/contact">Contact Us</a></li>
    <li><a href="/links">Links</a></li>
</ul>

<div id="page"></div>

JS: JS:

$(function(){

    /*
     * We override the default
     * behaviour of our links
     * and change the hash of the URL,
     * e.g. '/contact' -> '#contact',
     * so the address bar of the browser
     * would change to 
     * 'http://example.com#contact'
     */
    $('ul').find('a').click(function() {
        var hash = $(this).attr('href').replace('#', '');
        window.location.hash = hash;

        return false;
    });

    /*
     * The main hashchange logic
     *
     * We use jQuery.load to retrieve
     * a specific part of the loaded document,
     * #page here
     */
    $(window).hashchange(function() {
        var newLoc = window.location.hash.replace('#', '');

        $('#page').load('/' + newLoc + ' #page');
    });

    $(window).hashchange();

});

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

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