这里我有一个 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());
}
谢谢!
最佳答案
试试这个:
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;
};
fiddle :http://jsfiddle.net/QBsWy/8/
关于javascript - 如何在 html 中加载 javascript 函数(span 标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20228744/