如何在一年中的每个星期显示不同的文本

问题描述 投票:0回答:1

我有一个环境博客,我正在研究这个小工具,以显示一年中每个星期的环境事实。我已经测试了程序的各个部分以确保它有效,但是,当我将这些部分组合起来时它不起作用。因为这个原因,我还没有完成所有的事实。这是我的节目

<script>
    var fact = new Array();
    fact[1] = "Only 1% of our planet’s water supply can be used. 97% is ocean water and 2% is frozen solid in the Arctic, for now.";
    fact[2] = "Aluminum can be recycled continuously, as in forever. Recycling 1 aluminum can save enough energy to run our TVs for at least 3 hours. 80 trillion aluminum cans are used by humans every year."; 
    fact[3] = "Rainforests are cut down at a rate of 100 acres per minute.";
    fact[4] = "In the past 50 years, humans have consumed more resources than in all previous history."; 
    fact[5] = "More than 100 billion pieces of junk mail are delivered in the United States each year, which comes out to 848 pieces per household. The production, distribution and disposal of all that junk mail creates over 51 million metric tons of greenhouses gases annually, the equivalent emissions of more than 9.3 million cars. "; 
    fact[7] = "Air pollution is the fourth largest risk factor for premature deaths."; 
    fact[8] = "Making copy paper from 100% recycled content fiber instead of 100% virgin forest fibers reduces total energy consumption by 44%, net greenhouse gas emissions by 38%, particulate emissions by 41%, wastewater by 50%, solid waste by 49% and wood use by 100%."; 
    fact[9] = ""; 
    fact[10] = ""; 
    fact[11] = ""; 
    fact[12] = ""; 
    fact[13] = ""; 
    fact[14] = ""; 
    fact[15] = ""; 
    fact[16] = ""; 
    fact[17] = ""; 
    fact[18] = ""; 
    fact[19] = ""; 
    fact[20] = ""; 
    fact[21] = ""; 
    fact[22] = ""; 
    fact[23] = ""; 
    fact[24] = ""; 
    fact[25] = ""; 
    fact[26] = ""; 
    fact[27] = ""; 
    fact[28] = ""; 
    fact[29] = ""; 
    fact[30] = ""; 
    fact[31] = ""; 
    fact[32] = "";
    fact[33] = "";
    fact[34] = "";
    fact[35] = "";
    fact[36] = "";
    fact[37] = "";
    fact[38] = "";
    fact[39] = "";
    fact[40] = "";
    fact[41] = "";
    fact[42] = "";
    fact[43] = "";
    fact[44] = "";
    fact[45] = "";
    fact[46] = "";
    fact[47] = "";
    fact[48] = "";
    fact[49] = "";
    fact[50] = "";
    fact[51] = "";
    fact[52] = "";
    Date.prototype.getWeekNumber = function(){
      var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
      var dayNum = d.getUTCDay() || 7;
      d.setUTCDate(d.getUTCDate() + 4 - dayNum);
      var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
      return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
      stuff.innerHTML = fact[yearStart]
    };
      stuff = document.getElementById('today'); 
<script/>
<p id="green">Today's Earth Fact Is:  <span id="today"> </span> </p>

“谢谢!

javascript html
1个回答
0
投票

线stuff.innerHTML = fact[yearStart]永远不会执行,因为它是在函数的return语句之后。

也许是一个糟糕的复制/粘贴但它应该在stuff = document.getElementById('today');之后

你也不应该从getWeekNumber里面做任务。该方法应该只返回其标题所暗示的周数。

var fact = new Array();
fact[1] = "Only 1% of our planet’s water supply can be used. 97% is ocean water and 2% is frozen solid in the Arctic, for now.";
fact[2] = "Aluminum can be recycled continuously, as in forever. Recycling 1 aluminum can save enough energy to run our TVs for at least 3 hours. 80 trillion aluminum cans are used by humans every year.";
fact[3] = "Rainforests are cut down at a rate of 100 acres per minute.";
fact[4] = "In the past 50 years, humans have consumed more resources than in all previous history.";
fact[5] = "More than 100 billion pieces of junk mail are delivered in the United States each year, which comes out to 848 pieces per household. The production, distribution and disposal of all that junk mail creates over 51 million metric tons of greenhouses gases annually, the equivalent emissions of more than 9.3 million cars. ";
fact[7] = "Air pollution is the fourth largest risk factor for premature deaths.";
fact[8] = "Making copy paper from 100% recycled content fiber instead of 100% virgin forest fibers reduces total energy consumption by 44%, net greenhouse gas emissions by 38%, particulate emissions by 41%, wastewater by 50%, solid waste by 49% and wood use by 100%.";
fact[9] = "";
fact[10] = "";
fact[11] = "";
fact[12] = "";
fact[13] = "";
fact[14] = "";
fact[15] = "";
fact[16] = "";
fact[17] = "";
fact[18] = "";
fact[19] = "";
fact[20] = "";
fact[21] = "";
fact[22] = "";
fact[23] = "";
fact[24] = "";
fact[25] = "";
fact[26] = "";
fact[27] = "";
fact[28] = "";
fact[29] = "";
fact[30] = "";
fact[31] = "";
fact[32] = "";
fact[33] = "";
fact[34] = "";
fact[35] = "";
fact[36] = "";
fact[37] = "";
fact[38] = "";
fact[39] = "";
fact[40] = "";
fact[41] = "";
fact[42] = "";
fact[43] = "";
fact[44] = "";
fact[45] = "";
fact[46] = "";
fact[47] = "";
fact[48] = "week 48";
fact[49] = "week 49";
fact[50] = "week 50";
fact[51] = "week 51";
fact[52] = "week 52";


Date.prototype.getWeekNumber = function() {
  var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate())),
      dayNum = d.getUTCDay() || 7,
      yearStart;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
  
  return Math.ceil((((d - yearStart) / 86400000) + 1) / 7)
};

var stuff = document.getElementById('today'),
    weekNumber = (new Date()).getWeekNumber();
stuff.innerHTML = fact[weekNumber];
<p id="green">Today's Earth Fact Is: <span id="today"> </span> </p>
© www.soinside.com 2019 - 2024. All rights reserved.