简体   繁体   English

使用Javascript创建不同的彩色警报消息

[英]Creating different coloured alert messages with Javascript

I'm trying to create a simple alert message, now what inparticular I'm trying to make is that if a user fails to login the alert message background-color is red, if a user successfully logs in the alert background-color is green. 我正在尝试创建一个简单的警报消息,现在我要特别说明的是,如果用户未能登录警报消息,背景颜色为红色,如果用户成功登录警报背景颜色为绿色。 I'm not sure how to make it so if the user succesfully logs in it uses the 'alertsuccess' div instead of the 'alert' div. 我不确定如何做到这一点,因此,如果用户成功登录,则使用“ alertsuccess” div而不是“ alert” div。 I'm not sure how to go about this so any help is appreciated 我不确定该如何处理,因此不胜感激

<div id="alert" style="display:none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">&  times;</span> 
<p id="alertmsg"> </p>
</div>

<div id="alertsuccess" style="display:none;">
 <span class="closebtn" onclick="this.parentElement.style.display='none';">&     times;</span> 
<p id="alertmsg"> </p>
</div>

 <input type="text" id="username" placeholder="Username" name="uname" maxlength="15" required>

<button id="button123" onclick="showalert('alert');">Submit</button>

My Javascript 我的Javascript

 function showalert(id){
 var divelement = document.getElementById(id);
 var username = document.getElementById("username").value;

   if (username === ""){
    document.getElementById("alertmsg").innerHTML = "You didn't insert a username!";
    divelement.style.display =='none'
    divelement.style.display = 'block';
    }

    else if(username === "u1460714"){
    document.getElementById("alertmsg").innerHTML = "Successfully logged   in";
    divelement.style.display =='none'
    divelement.style.display = 'block';
    }
    }

Finally my css 最后我的CSS

#alert {
height:100px;
width:300px;
background-color: #f44336;
color: white;
position: fixed;
}

.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}

.closebtn:hover {
color: black;
}

#alertmsg{
font-family:sans-serif;
font-size:15px;
text-align: center;
}

#alertsuccess {
background-color: #4CAF50;
}

Instead of using two div 's you can just set the color of the one. 除了使用两个div ,您还可以设置一个的颜色。 Then you won't run into issues where you have the same id twice. 这样就不会遇到两次具有相同ID的问题。

 function showalert(alert) { var alertBlock = document.getElementById(alert); var inputName = document.getElementById('username'); var messageBlock = document.getElementById('alertmsg'); alertBlock.style.display = 'block'; if (inputName.value == '') { alertBlock.style.backgroundColor = 'red'; messageBlock.innerHTML = 'No username entered'; } else if (inputName.value == 'u2345') { alertBlock.style.backgroundColor = 'green'; messageBlock.innerHTML = 'You entered the specific username'; } } 
 #alert { width: 200px; height: 200px; display: none; } 
 <div id="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">X</span> <p id="alertmsg">test</p> </div> <input type="text" id="username" placeholder="Username"name="uname" maxlength="15" required> <button id="button123"onclick="showalert('alert');">Submit</button> 

I think you have a typo in there 我想你那里有错字

divelement.style.display =='none';

should be = 'none' or it will do a compare. 应该为=“ none”,否则它将进行比较。 There are some other lines the same too. 其他行也一样。 Hope this helps 希望这可以帮助

What you could do is create a div (with your id) and change appearance based on the input by using css classes. 您可以做的是创建一个div(带有您的ID)并使用CSS类根据输入更改外观。 Instead of creating a new div beneath the alert div, we can simply change the appearance with the classes alert and success. 除了在alert div下方创建新的div之外,我们还可以使用alert和success类简单地更改外观。 The div with id 'alert' never changes, except for the classes, which you define in your javascript. 标识为“ alert”的div不会更改,除了您在javascript中定义的类之外。

Take a look at the css and especially at #alert, #alert.alert and #alert.success. 看一下CSS,尤其是#alert,#alert.alert和#alert.success。 A good thing to note is that the div with id 'alert' is hidden by default, this can be overruled in the javascript. 需要注意的一件好事是,默认情况下隐藏ID为“ alert”的div,这可以在javascript中否决。 Also try to avoid using id on element, because the element is less reusable, then when using classes. 还要避免在元素上使用id,因为在使用类时,元素的可重用性较低。

 function showalert(id) { var divelement = document.getElementById('alert'); var username = document.getElementById("username").value; if (username === "") { document.getElementById("alertmsg").innerHTML = "You didn't insert a username!"; divelement.style.display = 'block'; divelement.className = 'alert' } else if (username == "u1460714") { document.getElementById("alertmsg").innerHTML = "Successfully logged in"; divelement.style.display = 'block' divelement.className = 'success' } } 
 #alert { height: 100px; width: 300px; color: white; position: fixed; display: none; } #alert.alert { background-color: #f44336; } #alert.success { background-color: #4CAF50; } .closebtn { margin-left: 15px; color: white; font-weight: bold; float: right; font-size: 22px; line-height: 20px; cursor: pointer; transition: 0.3s; } .closebtn:hover { color: black; } #alertmsg { font-family: sans-serif; font-size: 15px; text-align: center; } 
 <div id="alert"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span> <p id="alertmsg"> </p> </div> <input type="text" id="username" placeholder="Username" name="uname" maxlength="15" required> <button id="button123" onclick="showalert('alert');">Submit</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM