第一次需要javascript帮助
有人能告诉我下面的javascript有什么问题吗。它假定显示时间和日期,但只显示HTML标题。我从一本我用来学习的javascript书中得到了这个信息,我想它可能已经过时了,事情可能已经改变了,或者我犯了一个错误 日期时间 时间和日期 现在=新日期; localtime=now.toString; utctime=now.togmString; document.write本地时间:+localtime+; document.write UTC时间:+utctime+; document.writeHello World!; 问题1。由于类型属性无效,无法将脚本块识别为JS,因此无法工作:第一次需要javascript帮助,javascript,Javascript,有人能告诉我下面的javascript有什么问题吗。它假定显示时间和日期,但只显示HTML标题。我从一本我用来学习的javascript书中得到了这个信息,我想它可能已经过时了,事情可能已经改变了,或者我犯了一个错误 日期时间 时间和日期 现在=新日期; localtime=now.toString; utctime=now.togmString; document.write本地时间:+localtime+; document.write UTC时间:+utctime+; document.
type="textjavascript"
应该是type=text/javascript
问题2。修复此问题后,请确保也修复此行:
+ utctime +);
// ^ ---- remove this "+"
问题3。把你读的书扔掉。完全过时了。原因:
不要使用-它是在非常特殊的情况下使用的,你的不是其中之一。
不要指定LANGUAGE=JavaScript和type=text/JavaScript,它们是多余的。
最后,学习DOM方法,如、、等,它们有很多
您可以用多种方式重写示例,例如:
日期时间
时间和日期
var now=新日期;
var localtime=now.toString;
var utctime=now.togmString;
var容器=document.querySelector'.date';
container.innerHTML=
本地时间:+localtime++
UTC时间:+utctime;
document.body.appendChilddocument.createTextNodeHello World!;
先换,然后
只需删除document.write中utctime之后的“+”,它就可以工作了,该变量没有要追加的字符串,因此不需要“+”
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<BR>");
document.write ("<b> UTC time: </b>" + utctime );
document.write("Hello World!");
这将起作用。您在下面的行中有问题
document.write ("<b> UTC time: </b>" + utctime +);
你终于添加了额外的+
因此,正确的工作代码应如下所示
只需删除以下语句末尾的+符号即可:
document.write ("<b> UTC time: </b>" + utctime +);
应该是:
document.write ("<b> UTC time: </b>" + utctime);
您的代码中有2个错误: MIME类型应设置为text/javascript,而不是textjavascript UTC时间:+utctime+中存在语法错误 代码中有一个多余的+。类型中也有一个错误。看看这个
<script type="text/javascript">
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<br>");
document.write ("<b> UTC time: </b>" + utctime);
document.write("Hello World!");
</script>
将type=textjavascript更改为type=text/javascript,并取出文档中的额外+。写入UTC时间:+utctime+ 试试这个:现在=新日期;localtime=now.toString;utctime=now.togmString;document.write本地时间:+localtime+;document.write UTC时间:+utctime;document.write UTC时间:+utctime+,这里有一个不必要的+悬空这也不起作用。我看不出你写的和我已有的有多大区别。我正在将文档保存为.html并上传到我的服务器,我认为这是正确的。谢谢Chris,这是textjavascript中的/缺失。我本以为我仔细检查了一下,但还是没看到。
<script type="text/javascript">
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<br>");
document.write ("<b> UTC time: </b>" + utctime);
document.write("Hello World!");
</script>