我想实现一个jquery移动导航。但是从jquery load()构建我自己的 功能。我看过history.js但很难理解。很高兴看到地址栏中页面的正确网址。请有人帮我推动我朝着正确的方向前进。
答案 0 :(得分: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,它会使用$('#content').load(State.url + " #content").html();
就是这样。