历史记录 api 状态更改函数脚本在每次发生更改时复制函数调用
History api statechange function script duplicates function calls each time change occurs
我在一个简单的演示中使用 jquery 历史插件,每次单击导航项时都会加载到特定页面的某个部分中,但我发现每次单击新链接时,History.Adapter.bind(window, 'statechange', function()
代码都是重复的。是否可以在每次代码在此函数中完成时解绑它,或者任何人都可以建议我可能出错的地方?
我的测试页面 http://kylehouston.com/_testing/history/page1.html
.JS
navLink.on('click', function(e) {
e.preventDefault();
var el = $(this);
//get the href and remove begining /
url = el.attr('href')
.slice(1);
//do a check to see if this link is already active
if (el.hasClass('is-active')) {
return false;
}
navLink.removeClass('is-active');
el.addClass('is-active');
History.pushState(null, null, url);
// Bind to StateChange Event
History.Adapter.bind(window, 'statechange', function() {
var State = History.getState();
mainContent.fadeOut(function() {
$(this)
.empty();
console.log('this ran');
preloader.fadeIn();
loadContent(mainContent, location.pathname);
});
History.Adapter.unbind();
});
//empty mainContent then empty it
mainContent.fadeOut(function() {
$(this)
.empty();
//display preloader
preloader.fadeIn();
//check if content already exists in pages{} if not then make call then save to pages{} or retrieve from pages{} if already exists
loadContent(mainContent, url);
});
});
你有没有试过移动
// Bind to StateChange Event
History.Adapter.bind(window, 'statechange', function() {
var State = History.getState();
mainContent.fadeOut(function() {
$(this).empty();
console.log('this ran');
preloader.fadeIn();
loadContent(mainContent, location.pathname);
});
});
以前
navLink.on('click', function(e){
相关文章:
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 如何在Javascript函数调用中循环变量
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 如何远程检查JavaScript应用程序的函数调用堆栈
- javascript函数调用不起作用
- 为什么这个函数调用会破坏程序并导致未定义的变量
- 如何通过函数调用设置图像的src
- 从全局函数调用Ember控制器上的方法
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- JavaScript函数调用(arg1)(arg2)
- 打印链接时,将javascript函数调用到链接中
- 在函数调用中封装数据除了隐藏数据之外还有什么优点
- 无法从JavaScript中的函数调用对象属性
- 在Meteor中如何将数据从函数内部复制到其他模板
- 对中的函数调用进行排序是回调的唯一方法
- 函数调用方法有什么用
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- HTML5(Bootstrap)通过函数调用运行动画
- 历史记录 api 状态更改函数脚本在每次发生更改时复制函数调用