用document.getelementbyid替换document.write

时间:2014-05-25 02:34:34

标签: javascript html

我试图在网站上添加当前日期/时间。我在这里找到了线索:date time with document.getElementById not working,代码我使用引用,但我正在寻找不同的输出。请参阅下面的代码。基本上,我试图将输出推送到跨页以在页面上显示。我现在所拥有的不起作用。提前感谢您提供的任何帮助。

<!doctype html>
<html>
<head>
<title>Date Time Script</title>
</head>
<body>

<span id="spanDate"></span>

<script type="text/javascript">


// Get today's current date.
var now = new Date();

// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Calculate the number of the current day in the week.
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

// Calculate four digit year.
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
                            }
// Altered code
// Join it all together
document.getElementByID("spanDate").innerHTML = days[now.getDay()] + ", " +
     months[now.getMonth()] + " " +
     date + ", " +
     (fourdigits(now.getYear())) ;

//Original code
// Print out the data.
//document.write(today);

//  End -->
</script>





</body>
</html>

2 个答案:

答案 0 :(得分:2)

你有一个错字:

document.getElementByID("spanDate")...

应该是:

document.getElementById("spanDate")

这里是一个jsfiddle:http://jsfiddle.net/HWA4X/

答案 1 :(得分:0)

我认为你想要days[date%7]而不是days[now.getDay()]