0

我想<span class="mr-3 text-danger date">使用 Dayjs 将日期渲染到元素中。我该怎么做呢?我在 html 文档底部的脚本标记中从 CDN 导入了 dayjs。

<!--This is my html code-->
<div class="d-flex event">
    <span class="mr-3 text-danger date"></span>
    <span class="text-danger">24°C, Fort Portal</span>
</div>
4

1 回答 1

1

<!DOCTYPE html>
<html>
    <body>
        <!--This is my html code-->
        <div class="d-flex event">
            <span class="mr-3 text-danger date"></span><br>
            <span class="text-danger">24°C, Fort Portal</span>
        </div>
    </body>
    <script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
    <script>

        let datesClasses = document.getElementsByClassName("date")

        Array.prototype.forEach.call(datesClasses, element => {
    
            element.innerHTML = dayjs().format('YYYY/MM/DD HH:mm:ss A')

        })
    </script>
</html>

于 2020-10-05T18:17:53.523 回答