簡體   English   中英

使用moment.js在HTML5中插入日期 <time> 元件

[英]Use moment.js to insert date inside HTML5 <time> element

我想使用moment.js在HTML5 <time>標記內插入一個回溯的RFC 3339時間戳,因此它將呈現為HTML:

<time datetime="2016-01-14T13:22:11-08:00">Lorem Ipsum</time>

使用moment.js,我的.js正常工作以生成時間戳:

document.getElementById('fourDaysEarlier').innerHTML = moment().subtract(4, 'days').format("YYYY-MM-DDTHH:mm:ssZ")

我可以用innerHTML成功寫入文檔,如下所示:

<span id="fourDaysEarlier"></span>

但是如何在<time>標簽內插入時間戳,就像這樣?

<time datetime="wantToInsertTimeStampHere">Lorem Ipsum</time>

提前致謝!

你可以像使用vanilla javascript setAttribute()方法一樣

    document.getElementById('fourDaysEarlier').setAttribute("datetime", moment().subtract(4, 'days').format("YYYY-MM-DDTHH:mm:ssZ"));

或者如果您使用的是jQuery - attr()方法

    $('#fourDaysEarlier').attr("datetime", moment().subtract(4, 'days').format("YYYY-MM-DDTHH:mm:ssZ") )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM