我的网站上有两种形式。一个 .login-form
和一个 .registration-form
。默认情况下,登录表单有一个 display: block
属性,注册表单有一个 display: none
属性。我有一个按钮可以在两种形式之间切换。已经很好用了。
现在我想根据 url 哈希切换这些属性。因此,如果您访问 /user.html#registration
,我想首先查看注册表而不是登录表单。
我怎样才能做到这一点? 非常感谢您的帮助!
亚罗。
最佳答案
读取哈希的简短脚本应该做的:
<script>
// Add this block to where your "after page load" code starts.
// If you use a framework, you should adjust the solution to your framework.
if (location.hash && location.hash === "#registration") {
// or whichever way your app switches between the two
document.body.classList.add("show-registration");
} // else it'll show login, as intended.
</script>
如果您的 CSS 尚未设置:
<style>
body.show-registration .login-form {
display: none;
}
body.show-registration .registration-form {
display: block;
}
</style>
关于javascript - 根据#hash 添加/删除 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37612984/