简体   繁体   English

可能是一个简单的修复,但不能修复自己

[英]probably an easy fix but cant fix myself

anyone know what I'm doing wrong with this, id like to make a welcome message that displays on startup which asks you your name and then welcomes you with a greeting dependent on the current time.任何人都知道我做错了什么,我喜欢在启动时显示一条欢迎消息,询问您的姓名,然后根据当前时间用问候语欢迎您。

The issue I'm having is that the prompt will work if everything else is comment blocked but also won't display on to the page in white text我遇到的问题是,如果其他所有内容都被评论阻止,提示将起作用,但也不会以白色文本显示在页面上

    var name
    name = prompt("Please enter your name.");

    document.getElementById("demo").innerHTML = (msg);
    msg.style.color = 'White';

    var now = Date();
    var hrs = now.getHours();
    var msg = "";

    if (hrs >  6) msg = "Good morning";   
    if (hrs > 12) msg = "Good afternoon";   
    if (hrs > 17) msg = "Good evening"; 
  1. you used msg before its declared您在声明之前使用了 msg
  2. msg don't have style property as it's not an element msg 没有 style 属性,因为它不是一个元素
  3. you never used name你从没用过名字
  4. you used white color, which is same as default page background color您使用了白色,与默认页面背景颜色相同
  5. you assigned value to innerHTML before its assigned to msg variable您在将值分配给 msg 变量之前将值分配给了innerHTML
  6. date object should be created with new keyword var now = new Date();日期对象应该用 new 关键字var now = new Date();创建var now = new Date();
var name = prompt("Please enter your name."); 
var msg = "";
console.log(name);
 document.getElementById("demo").style.color = 'red'; // use other color because white is default background color and your text will not be visible
var now =  new Date(); 
var hrs = now.getHours(); 
console.log(hrs);
if (hrs > 0) 
  msg = "Good early morning "+ name; 
else if (hrs > 6) 
  msg = "Good morning "+ name; 
else if (hrs > 12) 
  msg = "Good afternoon "+ name; 
else if (hrs > 17) 
  msg = "Good evening "+ name; 
console.log(msg);
document.getElementById("demo").innerHTML = msg;

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

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