有人可以帮助我在车把文件中获取今天的日期吗? 输出应该是"今天是星期四" 。 `
<script>
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
var x = document.getElementById("MyP").innerHTML;
x = x + daylist[day];
document.getElementById("MyP").innerHTML = x;
</script>
<h2 class="page-header">Dashboard</h2>
<p>Welcome to {{user.username}}</p>
<p id="MyP"></p>
`
我一直在尝试这种方式,但某种程度上它不起作用
答案 0 :(得分:1)
试试这个。确保<script>
标记位于HTML之后,否则当脚本执行时,HTML尚未就绪。
<h2 class="page-header">Dashboard</h2>
<p>Welcome to {{ user.username }}</p>
<p id="MyP">Today is </p>
<script>
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
document.getElementById("MyP").innerHTML += daylist[day];
</script>