javascript - Hashchange jQuery 插件 - 单击 anchor 时获取当前哈希值

标签 javascript jquery jquery-plugins jquery-events

我正在使用 http://benalman.com/projects/jquery-hashchange-plugin/ 的 hashchange jQuery 插件当窗口的 location.hash 更改时 Hook 事件。

我想在哈希更改时触发一个函数,传递新的哈希值(通过 event.fragment 获得)和当前哈希值(事件触发之前的值) .

这是我想要实现的目标的一个片段:

$(window).bind('hashchange', function(event){
   myFunction(event.fragment, /* currentHash */);
});

这可能吗?

最佳答案

该位置有一处特性:

$(window).bind('hashchange', function(event){
   myFunction(event.fragment, location.hash);
});

或者,自己存储:

var lastHash = location.hash;                 //set it initially
$(window).bind('hashchange', function(event){
   myFunction(event.fragment, hashLash);      //previous hash
   lastHash = location.hash;                  //update it
});

关于javascript - Hashchange jQuery 插件 - 单击 anchor 时获取当前哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4308723/

相关文章:

javascript - jQuery 检索最接近唯一的选择器

javascript - 我可以使用Chrome调试协议(protocol)设置worker的脚本源吗?

javascript - express.js 应用程序没有响应

javascript - 三.js : Box3 from an object not in scene

Javascript 和 jquery 命名空间

php - 更新 Facebook 上的数据库字段 "Like"

javascript - 使用jquery自动扩展文本区域

javascript - 计算鼠标和 DOM 元素边缘之间的距离

jQuery 获取第 n 个元素的值

php - WordPress 中的 Lumia-bxslider(lumia-bxslider WordPress 插件)