我想实现一个 jquery 移动导航。但是从 jquery load() 函数构建我自己的。我看过history.js,但它很难理解。很高兴在地址栏中看到页面的正确 URL。请有人可以帮助我朝着正确的方向前进。
问问题
144 次
1 回答
0
这是一个简单的 History.js 示例。
我们的标记。
<!DOCTYPE HTML>
<html>
<head>
<script src="jquery.js"></script>
<script src="jquery.history.js"></script>
<title>History.js</title>
</head>
<body>
<a href="page1.html">Page 1</a>
<a href="Page2.html">Page 2</a>
<div id="content">
<p>Content within this box is replaced with content from
supporting pages using javascript and AJAX.</p>
</div>
</body>
</html>
javascript。
$(function() {
// Note: We are using a capital H instead of a lower 'h'
var History = window.History;
if (!History.enabled) {
// History.js is disabled for this browser.
// This is because we can optionally choose to support HTML4 browsers or not.
return false;
}
// Bind to StateChange Event
History.Adapter.bind(window, 'statechange', function(e) {
// Note: We are using statechange instead of popstate
var State = History.getState();
$('#content').load(State.url + " #content").html();
});
$('a').on('click',function(evt) {
evt.preventDefault();
History.pushState(null, $(this).text(), $(this).attr('href'));
});
});
将该脚本插入到页面的头部标签或底部。基本上 pages1.html 和 page2.html 应该包含一个名为 content 的 div,它会使用 load from 方法返回内容$('#content').load(State.url + " #content").html();
就是这样。
于 2013-02-21T10:07:49.573 回答