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

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

谢谢!

最佳答案

试试这个:

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/

相关文章:

javascript - 有没有办法简单地在crm中加载记录?

javascript - 使用jsp动态包含时Onload可以提前触发吗?

javascript - 我改变不透明度的功能不起作用

javascript - 当 url 上已有 # anchor 时如何滚动到特定元素

jquery - owlCarousel 在 Prev 和 Next 上用动画反转幻灯片移动方向

html - 如何将图像宽度设置为 Bootstrap 列的宽度

html - 为什么当我的 div 在 body 外面时没有出现滚动条?

javascript - 点击每个类项目的功能

javascript - 使用 angularjs 将新元素添加到 DOM 中不会启动它

javascript - MongoDB 在本地运行还是通过 Atlas 运行?