[英]Trouble with setting greeting note with JS on HTML
I'm having trouble running a JS code from separate stylesheet on HTML page. 我无法从HTML页面上的单独样式表运行JS代码。 Basically, I want to set an adequate greeting messages depending on the current hour of the day.
基本上,我想根据一天中的当前时间设置适当的问候消息。 I've searched and there are many ways to do it, but I am trying to get it work in this way and I didn't find a solution to this problem.
我已经搜索过,并且有很多方法可以执行此操作,但是我试图以这种方式使其工作,但是我没有找到解决此问题的方法。
window.onload = getGreeting(); function getGreeting() { var time = new Date(); var curHours = time.getHours(); if (curHours < 10) { document.getElementById("greeting").innerHTML = "Dobro jutro!"; } if else (curHours < 20) { document.getElementById("greeting").innerHTML = "Dobar dan!"; } if else { document.getElementById("greeting").innerHTML = "Dobro vece!"; } };
<!DOCTYPE html> <head> <script type="text/javascript" src="LakaRukaDent.js"></script> </head> <body> <p id="greeting"></p> </body>
When I run the HTML in Chrome the greeting doesn't appear. 当我在Chrome中运行HTML时,问候语不会出现。 When I press f12 in Chrome it comes up with " Uncaught SyntaxError: Unexpected token < ", but I can't figure out why it doesn't work.
当我在Chrome中按f12键时,它会显示“ Uncaught SyntaxError:Unexpected token < ”,但我不知道为什么它不起作用。 I've checked syntax and functionality searching through the web but considering that I'm new with JS and HTML perhaps it could be some basic thing that I've skipped and I am not aware of.
我已经检查了通过网络搜索的语法和功能,但是考虑到我是JS和HTML的新手,也许这是我跳过的一些基本内容,但我并不知道。
Use else if
instead of if else
. else if
不是, if else
使用else if
。 Example from MDN: 来自MDN的示例:
if (x > 5) {
} else if (x > 50) {
} else {
}
Please find the fix below. 请在下面找到修复程序。
window.onload
expects a function reference not returned value of a function, that is why i remove parenthesis over there. window.onload
期望函数引用不返回函数值,这就是为什么我在那删除括号。 Also there was syntax error with if else
statement. if else
语句也存在语法错误。
window.onload = getGreeting; function getGreeting() { var time = new Date(); var curHours = time.getHours(); if (curHours < 10) { document.getElementById("greeting").innerHTML = "Dobro jutro!"; } else if (curHours < 20) { document.getElementById("greeting").innerHTML = "Dobar dan!"; } else { document.getElementById("greeting").innerHTML = "Dobro vece!"; } };
<h1 id="greeting"></h1>
First off, remove the parentheses from getGreeting
when assigning to onload
, as onload
accepts a function. 首先,在分配给
onload
,请从getGreeting
删除括号,因为onload
接受一个函数。 Also, you have syntax errors with your if statements. 另外,如果if语句存在语法错误。 It should be
else if
not if else
like so: 它应该是
else if
没有if else
像这样:
window.onload = getGreeting; function getGreeting() { var time = new Date(); var curHours = time.getHours(); if (curHours < 10) { document.getElementById("greeting").innerHTML = "Dobro jutro!"; } else if (curHours < 20) { document.getElementById("greeting").innerHTML = "Dobro dan!"; } else { document.getElementById("greeting").innerHTML = "Dobro vece!"; } }
<p id="greeting"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.