如何使用jQuery根据哈希值触发不同的哈希变化函数?

发布于 2024-11-10 14:12:09 字数 584 浏览 4 评论 0原文

我正在使用 Ben AlmanjQuery hashChange 事件插件。 哈希部分如下所示:

#x=1&y=2&z=3

我已经有了解析它的函数 getHashPart(key)setHashPart(key)

我想要一个函数,允许仅注册哈希的特定部分的更改。 我会这样使用它(我猜......):

$(window).hashchange('x',function(new_value_of_x) {do something x related;});
$(window).hashchange('y',function(new_value_of_y) {do something y related;});

我如何使用 jQuery 注册这样的函数? 存在类似的东西吗?

谢谢!

I'm using Ben Alman's jQuery hashChange event plugin.
the hash part looks like this:

#x=1&y=2&z=3

I already have functions that parse it getHashPart(key) and setHashPart(key).

I would like to have a function that would allow to register with the change of only a specific part of the hash.
I would use it as such (I guess...):

$(window).hashchange('x',function(new_value_of_x) {do something x related;});
$(window).hashchange('y',function(new_value_of_y) {do something y related;});

How would I register such a function with jQuery?
anything similar exists?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-11-17 14:12:09

您可以拥有一个响应所有 hashchange 事件的函数(最好保持使用 hashchange 插件),然后重定向到单个处理程序,如下所示:

var hashParameters = {};

$(window).bind("hashchange",function(event) {
//if you're using the awesome hashchange plugin
//$(window).hashchange(function(event) { ...
    var newParameters = getParameters(event.target.location.hash.substring(1));
    $.each(newParameters, function(key, value) {
       if(hashParameters[key] !== value) $(window).trigger(key + "-change");
       hashParameters[key] = value;
    });
});

$(window).bind("andsome-change", function() {
    alert("hi!");
});

function getParameters(paramString) {
    //taken from http://stackoverflow.com/questions/4197591/parsing-url-hash-fragment-identifier-with-javascript/4198132#4198132
    var params = {};
    var e, a = /\+/g, r = /([^&;=]+)=?([^&;]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); };
    while (e = r.exec(paramString))
       params[d(e[1])] = d(e[2]);
    return params;
}

window.location.hash = "some=value&andsome=othervalue";

我有一个工作演示 这里(现在带有 hashchange 插件,请参阅第一条评论)。

You can have a function that responds to all hashchange events (preferably keeping the hashchange plugin in use), then redirect to individual handlers like this:

var hashParameters = {};

$(window).bind("hashchange",function(event) {
//if you're using the awesome hashchange plugin
//$(window).hashchange(function(event) { ...
    var newParameters = getParameters(event.target.location.hash.substring(1));
    $.each(newParameters, function(key, value) {
       if(hashParameters[key] !== value) $(window).trigger(key + "-change");
       hashParameters[key] = value;
    });
});

$(window).bind("andsome-change", function() {
    alert("hi!");
});

function getParameters(paramString) {
    //taken from http://stackoverflow.com/questions/4197591/parsing-url-hash-fragment-identifier-with-javascript/4198132#4198132
    var params = {};
    var e, a = /\+/g, r = /([^&;=]+)=?([^&;]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); };
    while (e = r.exec(paramString))
       params[d(e[1])] = d(e[2]);
    return params;
}

window.location.hash = "some=value&andsome=othervalue";

I have a working demo here (now with hashchange plugin, see first comment).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文