0

我正在尝试调用给定哈希字符串中的函数名称的函数。我有以下代码:

$(window).on('hashchange', function() {
  //alert(location.hash.substring(1, location.hash.length));
  window[location.hash.substring(1, location.hash.length)];
});

function test() {
  alert('123!');
}

有趣的是,当我取消注释alert呼叫时,一切都按预期工作。但是,当alert被评论时,它不起作用。

有任何想法吗?

4

1 回答 1

4
window[location.hash.substring(1, location.hash.length)];

不调用函数。

如果你想调用名字为 的函数location.hash.substring(1, location.hash.length),你可以这样做

window[location.hash.substring(1, location.hash.length)]();

边注 :

location.hash.substring(1, location.hash.length)

可以缩短为

location.hash.slice(1)
于 2012-12-20T19:04:40.027 回答