3 回答
TA贡献1799条经验 获得超9个赞
您可以使用insertAdjacentHTML,这是document.write与 with最接近的等效替换currentScript来获取正在评估的脚本的 HTML 元素。
<p>Updated
<script>
var modiDate = new Date(document.lastModified);
var monthName = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
var text = monthName[modiDate.getMonth()] + " ";
text += modiDate.getDate() + ", " + modiDate.getFullYear();
document.currentScript.insertAdjacentHTML('afterend', text);
</script>
</p>
TA贡献1851条经验 获得超5个赞
使用getElementById和toLocaleDateString
// date.js
document.getElementById('updated-date').textContent = new Date().toLocaleDateString('en-US', { /* new Date(document.lastModified)... */
month: 'long',
day: 'numeric',
year: 'numeric'
})
<p>Updated <span id="updated-date"></span></p>
<!-- end of body
<script type="text/javascript" src="/public/jsscripts/date.js">-->
添加回答
举报