gpt4 book ai didi

jquery - 这是使用 History.js 的正确方法吗?

转载 作者:行者123 更新时间:2023-12-03 22:20:12 26 4
gpt4 key购买 nike

我能够使用三个链接来组合一个简化的完整 History.js 示例,从整个页面加载内容片段,而无需更新页面,同时更新浏览器历史记录。

以下是相关代码片段 - 完整的工作示例在这里 http://jsfiddle.net/PT7qx/show

<a href="/page-header">Page Header</a>
<a href="/login-form">Login Form</a>
<a href="/nothing">Nothing</a>

<script>
var History = window.History;
if (!History.enabled) {
return false;
}

History.Adapter.bind(window, 'statechange', function() {
var State = History.getState();
History.log(State.data, State.title, State.url);

if (State.title == 'Page Header') {
$('#content').load('/user/login/ .pageHeader > *');
}

if (State.title == 'Login Form') {
$('#content').load('/user/login/ #common-form > *');
}

if (State.title == 'Nothing') {
$('#content').empty()
}
});

$('body').on('click', 'a', function(e) {

var urlPath = $(this).attr('href');
var Title = $(this).text();
History.pushState(null, Title, urlPath);

// prevents default click action of <a ...>
return false;
});
<\script>

我想知道这是否是正确的用法。以前的版本能够使用 # url 绑定(bind)到事件。我还没有看到任何将事件绑定(bind)到最新版本的 url 的示例,因此我使用 .on() 单击事件来调用历史记录并找出在那里单击的链接。

我不确定这是否是完成此示例的最佳方法。

最佳答案

经过更多研究后,我想出了一个简单但完整的示例来说明如何使用最新的 History.js。这是working jsfiddle example这会执行 Ajax 负载 HTML fragments hosted on Github

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Simple History.js Ajax example by dansalmo</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://balupton.github.com/history.js/scripts/bundled/html4+html5/jquery.history.js"></script>

<style type='text/css'>
.hidden {
display: none;
visibility: hidden;
}
</style>
</head>
<body>
<a href="/home">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
<a href="/other">Other</a>

<p>The whole page will not re-load when the content below is updated, yet the URL is clean and the back button works!<p><br />
<div id="content">
<div id="home">Home Page content</div>
</div>
<br />
<p>The content div will be updated with a selected div fragment from an HTML file hosted on github, however the broswer will see each content update request as a part of the page history so that the back button can be used.</p>
<br />
<p>Adding more menu items is as simple as adding the new links and their corresponding html fragments.<p>
<div id="hidden_content" class="hidden"></div>
</body>
<script type='text/javascript'>//<![CDATA[
$(function(){
var History = window.History;
if (History.enabled) {
State = History.getState();
// set initial state to first page that was loaded
History.pushState({urlPath: window.location.pathname}, $("title").text(), State.urlPath);
} else {
return false;
}

var loadAjaxContent = function(target, urlBase, selector) {
$(target).load(urlBase + ' ' + selector);
};

var updateContent = function(State) {
var selector = '#' + State.data.urlPath.substring(1);
if ($(selector).length) { //content is already in #hidden_content
$('#content').children().appendTo('#hidden_content');
$(selector).appendTo('#content');
} else {
$('#content').children().clone().appendTo('#hidden_content');
loadAjaxContent('#content', State.url, selector);
}
};

// Content update and back/forward button handler
History.Adapter.bind(window, 'statechange', function() {
updateContent(History.getState());
});

// navigation link handler
$('body').on('click', 'a', function(e) {
var urlPath = $(this).attr('href');
var title = $(this).text();
History.pushState({urlPath: urlPath}, title, urlPath);
return false; // prevents default click action of <a ...>
});
});//]]>

</script>
</html>

关于jquery - 这是使用 History.js 的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13278822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com