Javascript 在绑定事件之间传递参数

Javascript 在绑定事件之间传递参数,javascript,jquery,Javascript,Jquery,假设我有两个绑定事件—单击和哈希更改,如下所示(超级简化示例): 如何将引发click事件的元素的id传递给绑定的hashchange事件? 我见过如下示例,但无法使其正常工作: $('a').bind('click', ['some-data'], function(e) { location.hash = $(this).attr('id'); } $(window).bind('hashchange', function(e) { console.log(e.data[0

假设我有两个绑定事件—单击和哈希更改,如下所示(超级简化示例):

如何将引发click事件的元素的id传递给绑定的hashchange事件? 我见过如下示例,但无法使其正常工作:

$('a').bind('click', ['some-data'], function(e) {
    location.hash = $(this).attr('id');
}

$(window).bind('hashchange', function(e) {
    console.log(e.data[0]);
}

任何建议都很好…

不可能从
hashchange
事件处理程序内部找到启动哈希更改的锚点,因为事件是由
窗口本身触发的

hashchange
处理程序中,您可以检查
location.hash
的值,因为您以前使用
location.hash=$(this.attr('id')
将锚的id放入其中

$('a').bind('click', ['some-data'], function(e) {
    location.hash = $(this).attr('id');
}

$(window).bind('hashchange', function(e) {
    console.log(e.data[0]);
}
$(window).bind('hashchange', function(e) {
    console.log(location.hash.substr(1)); // should be the id of your anchor
}