jQuery:当url中有某个哈希时触发一个函数......?

Cor*_*ial 1 jquery function pageload

我想在页面加载url中的某个哈希时执行一个函数.

因此,如果我访问,www.mypage.com/hello.html#functionplease则代码将执行.

谁能指出我正确的方向?

任何建议赞赏!

Rob*_*lin 9

var h = location.hash.substr(1);
if (h == 'functionplease')
    functionplease();
Run Code Online (Sandbox Code Playgroud)

substr(1)删除#in哈希.

很多功能?没问题!

switch(location.hash.substr(1)) {
    case 'buy': buy(); break;
    case 'sell': sell(); break;
    case 'other': other(); break;
}
Run Code Online (Sandbox Code Playgroud)