Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
My Date()JavaScript函数不起作用?_Javascript - Fatal编程技术网

My Date()JavaScript函数不起作用?

My Date()JavaScript函数不起作用?,javascript,Javascript,我正在尝试根据时间向我的站点添加文本。然而,我不能让它工作 HTML: 我只是想能够将消息添加到所说的div。当我尝试该页面时,什么都没有出现。我尝试了不同的方法,那么,我缺少什么呢?而是设置innerHtml属性、text或html tag.innerHTML = timeAdd() 您的作业错误-应按以下方式完成: tag.textContent = timeAdd(); 但是,更好的支持有innerHTML。您可以像textContent一样使用它 tag.innerHTML = ti

我正在尝试根据时间向我的站点添加文本。然而,我不能让它工作

HTML:


我只是想能够将消息添加到所说的div。当我尝试该页面时,什么都没有出现。我尝试了不同的方法,那么,我缺少什么呢?

而是设置
innerHtml
属性、
text
html

tag.innerHTML = timeAdd()

您的作业错误-应按以下方式完成:

tag.textContent = timeAdd();
但是,更好的支持有
innerHTML
。您可以像
textContent
一样使用它

tag.innerHTML = timeAdd();
此外,您的开关不工作。您必须使用
开关(true)
,才能使其正常工作。就我个人而言,我宁愿选择一款对我来说更干净的
if-else

if (todayHour > 18){
   message = "Good Evening!";
} else if (todayHour > 12){
   message = "Good Afternoon!";
} else if (todayHour > 0){
   message = "Good Morning!";
} else {
   message = "Welcome!";
}
编辑:此外,您还必须注意脚本的执行顺序。通常,您可以在头部包含
index.js
,但是这部分:

var tag = document.getElementById("welcomeText");
tag.textContent =timeAdd();
必须在标记中引入
后执行。因此,要么在该元素之后调用
index.js
,要么在该元素之后执行这两行:

<h3 id="welcomeText"></h3>
<script src="index.js"></script>


var tag=document.getElementById(“welcomeText”);
tag.textContent=timeAdd();

textContent
是一个属性,而不是一个方法。
此外,如果首先使用布尔大小写进行匹配,则应使用
true
作为开关参数

试试这个:

var timeAdd = function(){
  var today = new Date();
  var todayHour = today.getHours();
  var message;
  switch(true){
    case(todayHour > 18):
      message = "Good Evening!";
      break;
    case(todayHour > 12):
      message = "Good Afternoon!";
      break;
    case(todayHour > 0):
      message = "Good Morning!";
      break;
    default:
      message = "Welcome!";
      break;
  }
  return message;
};

var tag = document.getElementById("welcomeText");
tag.textContent = timeAdd();

这里有几个问题。首先,从结束div标记中删除
id=“header”
;这是无效的HTML

其次,switch语句的工作方式与您认为的不同。改用它作为timeAdd函数(该名称也没有意义,可能是
getTimeGreeting

var timeAdd = function() {
    var today = new Date();
    var todayHour = today.getHours();
    var message = 'Welcome!';

    if (todayHour >= 18) {
        message = 'Good Evening!';
    } else if (todayHour >= 12) {
        message = 'Good Afternoon!';
    } else if (todayHour >= 0) {
        message = 'Good Morning!';
    }

    return message;
};

最后,将设置行更改为
tag.innerText=timeAdd();

tag.innerHTML=timeAdd()
应该可以工作。您想使用
开关(true)
@Antiga See@ColbyCox在发布有关JavaScript的问题之前,请尝试在浏览器中使用开发者工具控制台。在这种情况下,它会告诉您,
textContent
属性不是函数。@ColbyCox您还需要在加载DOM之前移动标记,因为JavaScript将在加载DOM之前执行。此外,你不是从哪里得到这些信息的?当然
h3
有textContent属性。那么你应该删除你的答案吗?非常感谢!我现在明白了。我感谢你的帮助。@ColbyCox如果你所有的问题现在都解决了,你可以通过接受答案作为解决方案来将你的问题标记为So社区已经解决他在分数的正下方打勾。
<script src="index.js"></script> <!-- without the last two lines! -->
<h3 id="welcomeText"></h3>
<script>
    var tag = document.getElementById("welcomeText");
    tag.textContent =timeAdd();
</script>
var timeAdd = function(){
  var today = new Date();
  var todayHour = today.getHours();
  var message;
  switch(true){
    case(todayHour > 18):
      message = "Good Evening!";
      break;
    case(todayHour > 12):
      message = "Good Afternoon!";
      break;
    case(todayHour > 0):
      message = "Good Morning!";
      break;
    default:
      message = "Welcome!";
      break;
  }
  return message;
};

var tag = document.getElementById("welcomeText");
tag.textContent = timeAdd();
var timeAdd = function() {
    var today = new Date();
    var todayHour = today.getHours();
    var message = 'Welcome!';

    if (todayHour >= 18) {
        message = 'Good Evening!';
    } else if (todayHour >= 12) {
        message = 'Good Afternoon!';
    } else if (todayHour >= 0) {
        message = 'Good Morning!';
    }

    return message;
};