我正在寻找一种自动更改哈希网址的方法。 (无需重新加载页面)
我想要它的原因是这样的:
我使用的是弹出登录/注册表单,它仅最初打开登录部分。点击登录后才能进入注册部分。因此,当用户单击 http://website.com/#modal-login从某个链接,我希望它重定向到 http://website.com/#register .
目前是直接进入#register
。有没有办法在用户点击登录后更改哈希 URL?
最佳答案
无需使用 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"
。
关于javascript - 有没有办法用 jQuery 自动更改哈希 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21949232/