[英]How to create dynamic pages using # ?
我注意到很多像Twitter和其他一些网站的网页都将AJAX融入他们的设计中。 引起我注意的一件事是使用#! 在URL中。 我想知道如何为自己或他们使用的方法做到这一点,谢谢!
你可以从非常简单的东西开始,使用Hashchange或BBQ插件。 阅读两者的手册,你将掌握这个想法。
这里有一个简短而通用的介绍: http : //code.google.com/intl/en-EN/web/ajaxcrawling/docs/html-snapshot.html
更新:
好吧,我们以Hashchange插件为例。 以下代码非常原始,但我认为这将有助于理解基本部分
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:
$(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.