如何在html(span标签)中加载javascript函数

时间:2013-11-26 21:52:41

标签: javascript html onload-event

这里我有一个HTML元素:

<h3>Current Date: <span id="spanDate"></span></h3>

我如何(在)上加载span标记

之间的函数
function aDate()
{
var d=new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
document.write(weekday[d.getDay()] + " ");
document.write(d.getDate() + ". ");
document.write(monthname[d.getMonth()] + " ");
document.write(d.getFullYear());
}

谢谢!

1 个答案:

答案 0 :(得分:3)

试试这个:

window.onload = function() {
    var d = new Date();
    var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var monthname = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    // Set your output to a variable
    var output = weekday[d.getDay()] + " " + d.getDate() + ". " + monthname[d.getMonth()] + " " + d.getFullYear();

    // Target the ID of the span and update the HTML
    document.getElementById('spanDate').innerHTML = output;
};

小提琴:http://jsfiddle.net/QBsWy/8/