更新 date.js 并删除 document.write

我想帮助更新此 date.js 代码以删除 document.write 语句,因为它现在已过时


 if (Date.parse(document.lastModified) != 0) {

  var modiDate = new Date(document.lastModified);

  var monthName = new Array("January", "February", "March", "April", "May", 

   "June", "July", "August", "September", "October", "November", "December");

  document.write(monthName[modiDate.getMonth()] + " ");

  document.write(modiDate.getDate() + ", " + modiDate.getFullYear());

  }

我用什么来代替 document.write(......请


javascript 代码通过此代码 snipit 在网页底部生成一个(文件)更新于 2019 年 9 月 12 日


 <p>Updated <script type="text/javascript" src="/public/jsscripts/date.js"> </script>

谢谢马克


凤凰求蛊
浏览 229回答 3
3回答

扬帆大鱼

您可以使用insertAdjacentHTML,这是document.write与 with最接近的等效替换currentScript来获取正在评估的脚本的 HTML 元素。<p>Updated&nbsp; <script>&nbsp; &nbsp; var modiDate = new Date(document.lastModified);&nbsp; &nbsp; var monthName = new Array("January", "February", "March", "April", "May",&nbsp; &nbsp; &nbsp; "June", "July", "August", "September", "October", "November", "December");&nbsp; &nbsp; var text = monthName[modiDate.getMonth()] + " ";&nbsp; &nbsp; text += modiDate.getDate() + ", " + modiDate.getFullYear();&nbsp; &nbsp; document.currentScript.insertAdjacentHTML('afterend', text);&nbsp; </script></p>

江户川乱折腾

使用getElementById和toLocaleDateString// date.jsdocument.getElementById('updated-date').textContent = new Date().toLocaleDateString('en-US', { /* new Date(document.lastModified)... */&nbsp; month: 'long',&nbsp; day: 'numeric',&nbsp; year: 'numeric'})<p>Updated <span id="updated-date"></span></p><!-- end of body&nbsp;<script type="text/javascript" src="/public/jsscripts/date.js">-->
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript