将日期和时间写入文档。 [英] Writing date and time to a document.

查看:70
本文介绍了将日期和时间写入文档。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我是JavaScript的新手,我只是试图让脚本运行

会写出完整的日期和时间查看每个网页。可以

有人指出我的代码中有什么错误,因为它似乎没有工作吗?我实际上是想知道代码的哪些部分做了什么以及为什么,

所以如果有人可以花时间解释它会非常感激。

这是我的脚本:


< script language =" javascript">

<! -


function datenTimes(){


var timeNow = new Date();


var day = timeNow.getDay();

var date = timeNow.getDate();

var month = timeNow.getMonth();

var minutes = timeNow.getMinutes();

var hours = timeNow.getHours();

var seconds = timeNow.getSeconds();


document.write(day +" ;,& nbsp;");

document.write(date);

document.write(month +",& nbsp;");

document.write(分钟+":");

document.write(小时+":");

document.write(seconds +"。");

document.write("此页面最近更新时间:" + document.lastModified);

}


datenTimes();


// - >

< / script>


问候,

CB

解决方案

Cerebral Believer写道:


大家好,


我是JavaScript的新手,我只是在尝试要获得一个脚本,

将在每个网页上查看完整的日期和时间。可以

有人指出我的代码中有什么错误,因为它似乎没有工作吗?



不起作用没有意义,你需要解释结果如何与你的预期不同。 ;-)脚本完全符合我期望的那样,所以没有帮助,我很害怕。


要了解JavaScript Date对象及其方法,一个好的开始

是官方的ECMAScript语言规范。 Mozilla

JavaScript指南&参考也不错:


< URL: http://www.mozilla.org/js/language/E262-3.pdf >

< URL: http://developer.mozilla.org/en/docs...ript_1.5_Guide >

< URL:
http://developer.mozilla.org/en/docs...l_Objects:Date


>


我实际上是想了解代码的哪些部分做什么以及为什么,

所以如果有人可以拿时间来解释它将非常感激。



使用上面的链接。对于

一般更广泛的日期处理,请尝试:


< URL: http://www.merlyn.demon.co.uk/js-dates.htm >


这是我的剧本:


< script language =" javascript">



语言属性已弃用,不能使用它。类型是必需的:


< script type =" text / javascript">


< ; -



不要在脚本元素中使用HTML注释。


> ;

函数datenTimes(){


var timeNow = new Date();


var day = timeNow .getDay();

var date = timeNow.getDate();

var month = timeNow.getMonth();

var minutes = timeNow.getMinutes();

var hours = timeNow.getHours();

var seconds = timeNow.getSeconds();



这些方法中的每一种都在ECMAScript规范中解释,还有

Netscape语言参考:


< URL:
http://developer.mozilla.org/en/docs...s:Date#Methods


>


document.write(day +",& nbsp;");

document.write(date );

document.write(month +",& nbsp;");

document.write(minutes +":");

document.write(hours +":");

document.write(seconds +"。");

文件。写(此页面最后更新:" + document.lastModified);



对document.write的多次调用效率低,

构建字符串并调用document.write一次要好得多。把它放在一个函数中也不是一个好的想法,因为它几乎总是被用作页面加载的

,而不是之后(尝试调用函数onload)并看到

会发生什么)。如果你在全局范围内执行这个代码,你会得到完全相同的结果。


document.write(

天+",& nbsp;" + date + month +",& nbsp;"

+ hours +" ;:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

+"。此页面最后更新:"

+ document.lastModified

);

-

Rob




" RobG" < rg *** @ iinet.net.auwrote in message

news:11 ********************** @ i42g2000cwa。 googlegr oups.com ...


Cerebral Believer写道:


>大家好,

我是JavaScript的新手,我只想尝试一个脚本,
将在每个网页上查看完整的日期和时间。可以
任何人指出我的代码中有什么错误,因为它似乎没有工作吗?



不起作用没有意义,你需要解释结果如何与你的预期不同。 ;-)脚本完全符合我期望的那样,所以没有帮助,我很害怕。



你好Rob,


对不起,这很模糊。基本上脚本不运行,没有错误代码或

任何类型的原因指示。当我加载页面时,没有任何内容写入

文档。


要了解JavaScript Date对象及其方法,一个好的开始

是官方ECMAScript语言规范。 Mozilla

JavaScript指南&参考也不错:


< URL: http://www.mozilla.org/js/language/E262-3.pdf >

< URL: http://developer.mozilla.org/en/docs...ript_1.5_Guide >

< URL:
http://developer.mozilla.org/en/docs...l_Objects:Date


>>



>我实际上是在试着学习代码的哪些部分做什么以及为什么,
所以如果有人能花时间解释它会非常感激。



使用上面的链接。对于

一般更广泛的日期处理,请尝试:


< URL: http://www.merlyn.demon.co.uk/js-dates.htm >


>这是我的剧本:

< script language =" javascript">



语言属性已弃用,不能使用它。类型是必需的:


< script type =" text / javascript">


> ;<! -



不要在脚本元素中使用HTML注释。



我认为这是一个标签,允许无法读取JavaScript的浏览器

ingnore代码。至少那是我相信我在我的

课程上教过的。我将不得不查看我的课程材料。


> function datenTimes(){

var timeNow = new Date();

var day = timeNow.getDay();
var date = timeNow.getDate();
var month = timeNow.getMonth();
var minutes = timeNow.getMinutes();
var hours = timeNow.getHours();
var seconds = timeNow.getSeconds();



这些方法中的每一种都在ECMAScript规范中解释,并且还有

Netscape语言参考:


< URL:
http://developer.mozilla.org/en/docs...s:Date#Methods


>>



> document.write(day +",& nbsp;");
document.write (日期);
document.write(month +",& nbsp;");
document.write(minutes +":");
document.write( hours +":");
document.write(seconds +"。");
document.write(" This页面最后更新时间:" + document.lastModified);



对document.write的多次调用都是低效的,

构建字符串并调用document.write一次要好得多。把它放在一个函数中也不是一个好的想法,因为它几乎总是被用作页面加载的

,而不是之后(尝试调用函数onload)并看到

会发生什么)。如果你在全局范围内执行这个代码,你会得到完全相同的结果。


document.write(

天+",& nbsp;" + date + month +",& nbsp;"

+ hours +" ;:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

+"。此页面最后更新:"

+ document.lastModified

);



鲍勃,非常感谢这个信息,我会阅读并提高我的b / b
知识。谢谢。


问候,

CB


Cerebral Believer写道:


RobG写道:


> Cerebral Believer写道:



< snip>


>< script type =" text / javascript">


>><! -


不要在脚本元素中使用HTML注释。



我认为这是一个标签



HTML SCRIPT元素的内容是CDATA所以包含任何内容

不能是一个标签。


允许浏览器无法阅读

JavaScript来代码。



只有不知道SCRIPT元素的浏览器才会尝试将b * b $ b的内容解释为标记。识别SCRIPT

元素但不支持脚本的浏览器知道足以忽略SCRIPT元素的内容

,因为这是来自IE的每个浏览器

3 / Netscape 2浏览器肯定不再存在问题

我不知道SCRIPT元素是什么。


至少我认为我已经在我的课程中教授了



它可能就是你所教授的,但技术标准

的网络开发教育课程是如此之差,以至于得到了什么教授

不一定代表知识。


我将不得不复习我的课程资料。



如果您的课程材料是在21世纪编写的,并且在SCRIPT元素中使用类似HTML注释的序列建议

那么您可以考虑

作为认真追求任何技术价值的理由

它包含。


Richard。


Hi all,

I am a newbie to JavaScript and am just trying to get a script going that
will write the ful date and time to each webpage as it is viewed. Can
anyone point out what mistakes there are in my code, as it does not seem to
work? I am actually trying to learn what parts of the code do what and why,
so if anyone can take the time to explain it would be much appreciated.
Here is my script:

<script language="javascript">
<!--

function datenTimes() {

var timeNow = new Date();

var day = timeNow.getDay();
var date = timeNow.getDate();
var month = timeNow.getMonth();
var minutes = timeNow.getMinutes();
var hours = timeNow.getHours();
var seconds = timeNow.getSeconds();

document.write(day + ",&nbsp;");
document.write(date);
document.write(month + ",&nbsp;");
document.write(minutes + ":");
document.write(hours + ":");
document.write(seconds + ".");
document.write("This page was last updated:" + document.lastModified);
}

datenTimes();

//-->
</script>

Regards,
C.B.

解决方案

Cerebral Believer wrote:

Hi all,

I am a newbie to JavaScript and am just trying to get a script going that
will write the ful date and time to each webpage as it is viewed. Can
anyone point out what mistakes there are in my code, as it does not seem to
work?

"Does not work" is meaningless, you need to explain how the result is
different from what you expected. ;-) The script does exactly what
I''d expect it to, so no help there I''m afraid.

To learn about the JavaScript Date object and its methods, a good start
is the official ECMAScript Language specification. The Mozilla
JavaScript guide & reference aren''t bad either:

<URL: http://www.mozilla.org/js/language/E262-3.pdf >
<URL: http://developer.mozilla.org/en/docs...ript_1.5_Guide >
<URL:
http://developer.mozilla.org/en/docs...l_Objects:Date

>

I am actually trying to learn what parts of the code do what and why,
so if anyone can take the time to explain it would be much appreciated.

Use the links above. For much more extensive treatement of dates in
general, try:

<URL: http://www.merlyn.demon.co.uk/js-dates.htm >

Here is my script:

<script language="javascript">

The language attribute is deprecated, don''t use it. Type is required:

<script type="text/javascript">

<!--

Do not use HTML comments inside script elements.

>
function datenTimes() {

var timeNow = new Date();

var day = timeNow.getDay();
var date = timeNow.getDate();
var month = timeNow.getMonth();
var minutes = timeNow.getMinutes();
var hours = timeNow.getHours();
var seconds = timeNow.getSeconds();

Each of these methods is explained in the ECMAScript spec and also the
Netscape language reference:

<URL:
http://developer.mozilla.org/en/docs...s:Date#Methods

>

document.write(day + ",&nbsp;");
document.write(date);
document.write(month + ",&nbsp;");
document.write(minutes + ":");
document.write(hours + ":");
document.write(seconds + ".");
document.write("This page was last updated:" + document.lastModified);

Multiple calls to document.write are inefficient, it is much better to
build a string and call document.write once. It is also not a good
idea to put it inside a function, as it must nearly always be used as
the page loads, not afterward (try calling the function onload and see
what happens). You will get exactly the same result if you execute
this code in a global context.

document.write(
day + ",&nbsp;" + date + month + ",&nbsp;"
+ hours + ":" + minutes + ":" + seconds
+ ". This page was last updated:"
+ document.lastModified
);
--
Rob



"RobG" <rg***@iinet.net.auwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...

Cerebral Believer wrote:

>Hi all,

I am a newbie to JavaScript and am just trying to get a script going that
will write the ful date and time to each webpage as it is viewed. Can
anyone point out what mistakes there are in my code, as it does not seem
to
work?


"Does not work" is meaningless, you need to explain how the result is
different from what you expected. ;-) The script does exactly what
I''d expect it to, so no help there I''m afraid.

Hi Rob,

Sorry, that was vague. Basically the script does not run, no error codes or
any kind of indication of why. When I load my page nothing is written to
the document.

To learn about the JavaScript Date object and its methods, a good start
is the official ECMAScript Language specification. The Mozilla
JavaScript guide & reference aren''t bad either:

<URL: http://www.mozilla.org/js/language/E262-3.pdf >
<URL: http://developer.mozilla.org/en/docs...ript_1.5_Guide >
<URL:
http://developer.mozilla.org/en/docs...l_Objects:Date

>>


> I am actually trying to learn what parts of the code do what and why,
so if anyone can take the time to explain it would be much appreciated.


Use the links above. For much more extensive treatement of dates in
general, try:

<URL: http://www.merlyn.demon.co.uk/js-dates.htm >

>Here is my script:

<script language="javascript">


The language attribute is deprecated, don''t use it. Type is required:

<script type="text/javascript">

><!--


Do not use HTML comments inside script elements.

I thought that was a tag to allow browsers that could not read JavaScript to
ingnore the code. At least that is what I believe I had been taught on my
course. I will have to review my course material.

>function datenTimes() {

var timeNow = new Date();

var day = timeNow.getDay();
var date = timeNow.getDate();
var month = timeNow.getMonth();
var minutes = timeNow.getMinutes();
var hours = timeNow.getHours();
var seconds = timeNow.getSeconds();


Each of these methods is explained in the ECMAScript spec and also the
Netscape language reference:

<URL:
http://developer.mozilla.org/en/docs...s:Date#Methods

>>


>document.write(day + ",&nbsp;");
document.write(date);
document.write(month + ",&nbsp;");
document.write(minutes + ":");
document.write(hours + ":");
document.write(seconds + ".");
document.write("This page was last updated:" + document.lastModified);


Multiple calls to document.write are inefficient, it is much better to
build a string and call document.write once. It is also not a good
idea to put it inside a function, as it must nearly always be used as
the page loads, not afterward (try calling the function onload and see
what happens). You will get exactly the same result if you execute
this code in a global context.

document.write(
day + ",&nbsp;" + date + month + ",&nbsp;"
+ hours + ":" + minutes + ":" + seconds
+ ". This page was last updated:"
+ document.lastModified
);

Bob, this info is very much appreciated, I''ll get reading and improve my
knowledge. Thanks.

Regards,
C.B.


Cerebral Believer wrote:

RobG wrote:

>Cerebral Believer wrote:

<snip>

><script type="text/javascript">

>><!--


Do not use HTML comments inside script elements.


I thought that was a tag

The contents of an HTML SCRIPT element are CDATA so anything contained
cannot be a tag.

to allow browsers that could not read
JavaScript to ingnore the code.

Only browsers that do not know what a SCRIPT element is would attempt to
interpret its contents as mark-up. Browsers that recognise the SCRIPT
element but do not support scripting know enough to ignore the contents
of SCRIPT elements, and as that is every browser from about IE
3/Netscape 2 there is certainly no longer an issue with browsers that
don''t know what a SCRIPT element is.

At least that is what I believe I
had been taught on my course.

It may well have been what you were taught, but the technical standards
of web-development educational courses is so poor that what gets taught
does not necessarily represent knowledge.

I will have to review my course material.

If your course material was written in the 21st century and proposes
using HTML-comment-like sequences in SCRIPT elements then you can regard
that as grounds for seriously questing the technical worth of anything
it contains.

Richard.


这篇关于将日期和时间写入文档。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆