前段时间我需要一个脚本来每周更新一些内容,我的问题在这个论坛。
A while ago I needed a script to update some content every week, and my question was answered in this forum.
-
现在,我不是像你这样的jQuery专业人员:),但我所遇到的问题是今天(这篇文章的那一刻)它的第52周,但脚本没有工作,它没有显示第52周的内容,所以我将我的HTML更改为第53周,并且它有效。
Now, and I'm not a jQuery pro like you :), but the "problem" I have is that today (the moment of this post) it's week #52 but the script wasn't working, it wasn't showing the content for week #52, so I changed my HTML to week #53, and it worked.
问题是没有第53周,所以我'我担心我将不得不改变我的HTML以继续计算第54周,第56周,#57等等,当那些星期不存在时。
The "problem" is that there's no week #53, so I'm afraid I'm going to have to change my HTML to continue counting for week #54, #56, #57 and so on, when those weeks don't exist.
以下是HTML的摘录(结构在几周内重复,当然内容更改):
Here's an extract of the HTML (the structure repeats over the weeks, content changes of course):
<div class="quotes-container quote-53"> <div class="quote">Quote here...</div> <div class="author">— Author </div> </div>脚本:
<script type="text/javascript"> Date.prototype.getWeek = function() { var onejan = new Date(this.getFullYear(),0,1); return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7); } jQuery(function(){ var today = new Date(); var weekno = today.getWeek(); jQuery('#quotes-wrapper').load('/common/testimonials.html div.quote-'+weekno); }); </script>知道发生了什么事吗?
非常感谢您对此的任何帮助。
Thanks a lot for any help on this.
推荐答案编辑:您使用的脚本认为星期日 - 星期六的周。由于一年并不总是在星期日开始,因此它将第一个部分周视为第1周。
The script you're using considers a "week" to be Sunday - Saturday. Since a year doesn't always start on a Sunday, it considers the first partial week to be week 1.
如果你只想要从第一个星期开始的7个星期年,请使用下面的脚本。如果你想要它基于星期日,那么你正在使用的脚本是正确的。
If you simply want 7 say periods since the first of the year, use the script below. If you want it based on Sunday, then the script you're using would be correct.
有一个星期#53 。这不是整整一周。
There is a week #53. It's just not a full week.
但我们在一周 52 。 我猜测当前的脚本没有说明闰年。
相反,你可以计算今年的日期,并将其除以7。
Instead you can calculate the day of this year, and divide that by 7.
示例: jsfiddle/etTS2/2/
<script type="text/javascript"> Date.prototype.getWeek = function() { var onejan = new Date(this.getFullYear(),0,1); var today = new Date(this.getFullYear(),this.getMonth(),this.getDate()); var dayOfYear = ((today - onejan +1)/86400000); return Math.ceil(dayOfYear/7) }; jQuery(function(){ var today = new Date(); var weekno = today.getWeek(); jQuery('#quotes-wrapper').load('/common/testimonials.html div.quote-'+weekno); }); </script>你仍会得到 53 在一年的最后一天(或闰年的最后两天)。
You'll still get a result of 53 on the last day of the year (or last two days in a leap year).
编辑:修正了 1 dayOfYear 。
更多推荐
jQuery:一年中的一周剧本表演
发布评论