[英]Turn on or off tag display in html
I have this CSS: 我有这个CSS:
div.row{
display: none;}
div.login {
height: 10em;
align-items: center;
display: flex;
justify-content: center;
margin: 0;
top: 50%;}
this HTML: 此HTML:
<div id="login" class="login">
<input type="password" style="width: 25%;" placeholder="Please enter your password">
<button onclick="logincheck()">Login</button>
</div>
<div id ='row' class="row">...</div>
and this JS: 和这个JS:
function logincheck(){
var pwd = document.getElementById('pw').value;
var curpwd = 'somevalue';
if(pwd === curpwd){
alert('You are now logged in!');
var mainpage = document.getElementById("row");
var logger = document.getElementById("login");
logger.style.display = "none !important";
mainpage.style.display = "block";
}else{
alert('Sorry the password was not recognized, please try again.');
}
}
Yet my page remains the same after the 'You are now logged in' window. 在“您现在已登录”窗口之后,我的页面仍然保持不变。 How do I make the login div disappear and make the row div appear?
如何使登录div消失并使行div出现? Also, the script works in chrome but now in explorer.
此外,该脚本可在chrome中使用,但现在可在资源管理器中使用。 Why?
为什么?
You are assigning pwd
to the DOM element #pw
, yet no element has such an ID. 您正在将
pwd
分配给DOM元素#pw
,但是没有元素具有这样的ID。 Your first step would be to add an ID of pw
to the <input>
element. 第一步是将ID为
pw
的元素添加到<input>
元素。
After this, all you need to do is change your style.display
value for logger
. 之后,您所需要做的就是更改
logger
的style.display
值。 You have the right idea, though please note that a value of none !important
is invalid in JavaScript. 您有一个正确的主意,不过请注意,在JavaScript中,
none !important
值无效。 Instead, simply go with none
. 相反,只需不进行
none
。 This works, and the !important
declaration is generally frowned anyway upon due to its maximum level of specificity . 这行得通,并且
!important
声明由于其最高的特异性通常反而会被皱眉。
In addition to the note about !important
, please note that it's also bad practice to use inline event handlers such as onclick=
. 除了有关
!important
的注释外,请注意,使用内联事件处理程序(例如onclick=
也是一种不好的做法。 I've updated this to use .addEventListener()
in my example: 我已在示例中将其更新为使用
.addEventListener()
:
var button = document.querySelector('#login button'); button.addEventListener("click", logincheck); function logincheck() { var pwd = document.getElementById('pw').value; var curpwd = 'somevalue'; if (pwd === curpwd) { alert('You are now logged in!'); var mainpage = document.getElementById("row"); var logger = document.getElementById("login"); logger.style.display = "none"; mainpage.style.display = "block"; } else { alert('Sorry the password was not recognized, please try again.'); } }
div.row { display: none; } div.login { height: 10em; align-items: center; display: flex; justify-content: center; margin: 0; top: 50%; }
<div id="login" class="login"> <input id="pw" type="password" style="width: 25%;" placeholder="Please enter your password"> <button>Login</button> </div> <div id='row' class="row">...</div>
Finally, please be aware that all front-end JavaScript is visible to users , even when obsfuscated. 最后,请注意,即使被混淆,用户也可以看到所有前端JavaScript。 As such, any visitor to this page will know that the password is
somevalue
. 这样,该页面的所有访问者都将知道密码为
somevalue
。 If this is for a secure page, you probably want to make use of a database and proper encryption instead of relying on front-end JavaScript. 如果这是用于安全页面,则您可能希望利用数据库和适当的加密,而不是依赖于前端JavaScript。
Hope this helps :) 希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.