有没有办法用jQuery自动更改哈希URL?

时间:2014-02-22 03:25:53

标签: javascript jquery

我正在寻找一种自动更改哈希网址的方法。 (没有页面重新加载)

我想要它的原因是:

我正在使用最初只打开登录部分的pop登录/注册表单。您只能在单击登录后进入注册部分。因此,当用户点击某个链接中的http://website.com/#modal-login时,我希望它重定向到http://website.com/#register

目前直接转到#register。有没有办法在用户点击登录后更改哈希URL?

1 个答案:

答案 0 :(得分:3)

无需使用jQuery

document.getElementById("modal-login").onClick = function () {
    window.location.hash = "register";
}

例如,尝试将其粘贴到浏览器的JavaScrtipt控制台中,然后单击您的问题文本。

document.getElementById("question").onclick = function() {
    window.location.hash = "footer";
}

如果你真的想出于某种原因使用jQuery

$('#modal-login').click(function(event) {
    event.preventDefault();
    window.location.hash = "register";
});

修改

您的问题不是关于哈希位置的一般情况,而是您使用的这个模式插件的工作原理。通过阅读插件的源代码来确定以下内容:

http://demo.pressapps.co/plugins/wp-content/plugins/pressapps-modal-login/js/modal-login.js?ver=1.0.0 http://demo.pressapps.co/plugins/wp-content/plugins/pressapps-modal-login/js/modal.js?ver=1.0.0

以下是您需要执行以获得所需行为的内容

$('.your-register-button-class').click(function(e) {
    /* We expect plugin's click handler to fire in addition to this one. */
    $(".modal-login-nav[href='#register']").click();
});

我假设.your-register-button-class的元素也有属性data-toggle="ml-modal"