如何使用日期方法在javascript中写入页面
How to write to page in javascript using date method
我想根据日期显示报价。示例:当天的报价是 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 = ' 每日报价 : ' + today+venomArray[today.getDay()-1];
检查这个。这可能有助于您的问题
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Class 6 array assignment starter file</title>
</head>
<body>
<section>
<h2 style = "color:red" id='expression'> Quote of the Day : <span id="quote"></span> </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>
<script type="text/javascript">
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] = "quote2";
venomArray[3] = "quote3";
venomArray[4] = "quote4";
var t = new Date();
//alert(t.getDay());
//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]);
document.getElementById("quote").innerHTML = venomArray[t.getDay()-1];
</script>
</body>
</html>
您可以使用元素的 innerHtml 属性来更新内容,如下所示:
document.getElementById('expression').innerHTML = "Quote";
这是纯JavaScript中的实现。您现在通常使用某种模板来显示网页中的可变文本。
使用这个:-
<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]);
/*Add this Code*/
var display_date = "today"+ venomArray[today.getDay()-1];
$('h2').html(display_date );
</script>
相关文章:
- 如何在javascript上使用flak-babel进行翻译
- 在javascript中使用命名空间
- 操作员”;新的“;根据我想在几个JavaScript文件中使用的类,在JavaScript中使用
- 在JavaScript中使用对象作为属性键
- 为什么不'我们在javascript中使用函数参数的数据类型
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 如何通过安全的https连接在javascript中使用基于soap xml的Web服务
- 如何在javascript中使用不止一个函数
- 在 Jquery/Javascript 中使用多个 OR (||) 运算符时如何设置变量
- 当null应该在javascript中使用而不是未定义时
- Javascript 变量使用 jquery 查找变量
- 避免在if condition-Javascript中使用函数
- 在javascript中使用split函数希望在页面加载时拆分url
- 在Javascript中使用全局变量作为缓存是很好的
- 在javascript中使用方括号表示法的嵌套对象
- 如何在javascript中使用嵌套函数作为生成器(使用“inner”yields)
- 如何分割由JavaScript中使用正则表达式的多个部分组成的字符串
- 在javascript中使用json数据导致问题
- 在javascript中使用小数
- 在 JavaScript 中使用重排隐藏元素,它与 CSS 隐藏属性有何不同