gpt4 book ai didi

javascript - 如何使用日期方法在javascript中写入页面

转载 作者:行者123 更新时间:2023-12-03 10:36:03 24 4
gpt4 key购买 nike

我想根据具体日期显示报价。示例:当天的报价是 3 月 10 日星期二:“在此处报价”。我有显示日期、日期和时间的警报,但我希望它显示在页面的 h2 标题中。因此,在加载警报后,我希望根据具体日期将报价写在页面上。

谢谢。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Class 6 array assignment starter file</title>

<script>

var venomArray = new Array();
venomArray[0] = " Ability is nothing without opportunity - Napoleon Bonaparte";
venomArray[1] = " Nothing happens unless first we dream - Carl Sandburg";
venomArray[2] = "quote3";

var today = new Date();
//var text = document.getElementById('expression').firstChild.nodeValue = venomArray[i];

// if I uncomment this, It displays the index 1 for tuesday. How do I get it to write to the page? Would I use firstChildnodeValue?
alert("The quote of the day for: "+today+ venomArray[today.getDay()-1]);


</script>
</head>

<body>
<section>
<h2 style = "color:red" id='expression'> Quote of the Day : </h2>



<h3> All of the Quotes are listed below:</h3>
<p>

Nothing happens unless first we dream - Carl Sandburg<br>
Believe you can and you're halfway there - Theodore Roosevelt<br>
A place for everything, everything in its place - Benjamin Franklin<br>
Don't let the fear of striking out hold you back - Babe Ruth<br>
We can't help everyone, but everyone can help someone - Ronald Reagan<br>
With self-discipline most anything is possible - Theodore Roosevelt
</p>
</section>
</body>
</html>

最佳答案

您需要设置元素的innerHTML属性

document.getElementById("expression").innerHTML = ' Quote of the Day : ' + today+venomArray[today.getDay()-1];

关于javascript - 如何使用日期方法在javascript中写入页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com