2013-11-26 71 views
1

在這裏我有一個HTML元素:如何在HTML加載javascript函數(span標記)

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

我怎麼能(上)加載下面的跨度標籤之間的功能

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()); 
} 

謝謝!

+2

喜歡這個? http://jsfiddle.net/QBsWy/ – PSL

+1

@PSL - 嵌入式JavaScript? EW。 – j08691

+1

@ j08691:D .....他正在使用document.write ... :)試試這種方式,http://jsfiddle.net/9Bnkc/ – PSL

回答

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/

相關問題