我已经成功实现了 jQuery BBQ 插件来构建快速原型(prototype),但是我遇到了一个与我的特定设置相关的小问题:
- 其中一个页面“#catalogue”包含使用函数“randomItems()”随机生成的项目网格,在 hashchange (当来自其他页面时)。
- 然后,用户点击网格中的任意项目即可查看#detailspage。
- 点击浏览器后退按钮后,我希望用户查看 #catalogue 页面(效果很好),但阻止页面在加载时生成一组新的随机项目(因此保留最后显示的所有项目)。
有没有办法知道用户按下了后退按钮,这样在这种情况下我就不会触发“randomItems()”函数?
最佳答案
您需要将 randomItems()
中的项目放入缓存中:
// save your records here in the cache
var items = [];
$(window).on('hashchange', function(){
var page = window.location.hash.replace(/^#/, '');
// check first if items has already records in it and check if page is #catalogue
// if not then call randomItems()
if (page === 'catalogue' && items.length === 0) {
items = randomItems();
}
// your code here follows
});
关于jquery bbq - 检测用户何时单击后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16592384/