我正在个人爱好网站上工作,并想在每个月的每一天都发布一种新的啤酒风格。我之前尝试过javascript但是很糟糕。我希望这个社区可以帮助我。我有一个最多31个阵列,每天都有不同的啤酒风格。但是,我不知道如何让啤酒风格每天都改变。我已经在互联网上搜索了两天试图解决这个问题,我已经走到了几乎让我的老鼠摆脱挫折的地步。任何帮助将不胜感激。如果我留下任何有助于你帮助我的重要信息,我将很乐意为你找回它。
我希望网站能够展示;
今天的啤酒风格是:"啤酒风格"
这是javascript:
var style = new Array();
style[0] = "Munich Helles";
style[1] = "Robust Porter";
style[2] = "American Wheat Beer";
style[3] = "American Wild Ale";
style[4] = "German Pilsner";
style[5] = "Czech Pilsner";
style[6] = "India Pale Ale";
style[7] = "English Brown Porter";
style[8] = "Kolsch";
style[9] = "Altbier";
style[10] = "Vienna Lager";
style[11] = "Baltic Porter";
style[12] = "Weissbier";
style[13] = "Oatmeal Stout";
style[14] = "Saison";
style[15] = "Belgian Dark Strong Ale";
style[16] = "California Common";
style[17] = "Russian Imperial Stout";
style[18] = "Belgian Dubbel";
style[19] = "Cream Ale";
style[20] = "English Brown Ale";
style[21] = "Doppelbock";
style[22] = "Rauchbier";
style[23] = "Lambic";
style[24] = "Gose";
style[25] = "Gueze";
style[26] = "Marzen";
style[27] = "Scwarzbier";
style[28] = "Pale Ale";
style[29] = "Irish Stout";
document.getElementById("dateBox").innerHTML=style[new Date().getUTCDate()];
以下是我的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- The Homebrewery
Author: Chris Stastny
Date: October 27th, 2015
-->
<meta charset="utf-8">
<title>The Homebrewery - Homebrewing</title>
<meta name="description" content="The Homebrewery - Homebrewing">
<meta name="author" content="Chris Stastny">
<link href="final.css" rel="stylesheet" type="text/css" />
<script src="modernizr-1.5.js"></script>
<script src="beerStyle.js"></script>
</head>
<header>
<h1> The Homebrewery</h1>
<nav>
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Equipment</a></li>
<li><a href="#">Brew Log</a></li>
<li><a href="#">Links</a></li>
<li><a href="#">Newsletter</a></li>
</ul>
</nav>
</header>
<body>
<h2>Greetings!</h2>
<div id="dateBox">
Today Beer Style is:
</div>
<br>
<img src="beer.jpg" alt="beer">
<p>This website was built to keep people informed about my homebrewing adventures. It will have brewday pictures, videos (possibly) in the future, recipes and general homebrewing information. There is also a newsletter that you can sign up for that will go into more detail about what is going on with my brewing.</p>
<br>
<footer>The Homebrewery - Homebrewing - 2015</footer>
</body>
</html>
答案 0 :(得分:2)
您可以使用.getUTCDate()
来获取当月的日期。它启动,因此您只需要减去一个,然后您可以将该值用作数组中的索引:
style[ new Date().getUTCDate() - 1 ];
如上所述,数组从索引0
开始,您的数组没有该索引的值,因此您可能希望使用索引0
到30
而不是1
来31
。根据您当前的结构,您不需要- 1
。
答案 1 :(得分:0)
var style = new Array();
style[1] = "Munich Helles";
style[2] = "Robust Porter";
style[3] = "American Wheat Beer";
style[4] = "American Wild Ale";
style[5] = "German Pilsner";
style[7] = "Czech Pilsner";
style[8] = "India Pale Ale";
style[9] = "English Brown Porter";
style[10] = "Kolsch";
style[11] = "Altbier";
style[12] = "Vienna Lager";
style[13] = "Baltic Porter";
style[14] = "Weissbier";
style[15] = "Oatmeal Stout";
style[16] = "Saison";
style[17] = "Belgian Dark Strong Ale";
style[18] = "California Common";
style[19] = "Russian Imperial Stout";
style[20] = "Belgian Dubbel";
style[21] = "Cream Ale";
style[22] = "English Brown Ale";
style[23] = "Doppelbock";
style[24] = "Rauchbier";
style[25] = "Lambic";
style[26] = "Gose";
style[27] = "Gueze";
style[28] = "Marzen";
style[29] = "Scwarzbier";
style[30] = "Pale Ale";
style[31] = "Irish Stout";
var d = new Date();
var n = d.getDate();
stuff = document.getElementById('today');
stuff.innerHTML = style[n]
&#13;
<p id="beer">Today's Beer Style Is:<span id="today"> </span>
</p>
&#13;
&LT;&GT;