jQuery:一年中的一周脚本正在运行

发布于 2024-10-09 18:37:37 字数 1088 浏览 0 评论 0原文

不久前,我需要一个脚本来每周更新一些内容,我的问题在 此论坛

--

现在,我不是像你一样的 jQuery 专业人士:),但我遇到的“问题”是今天(这篇文章的时刻)是第 52 周,但脚本不起作用,它没有显示第 52 周的内容,因此我将 HTML 更改为第 53 周,并且它起作用了。

“问题”是没有第 53 周,所以我担心我必须更改我的 HTML 才能继续计算第 54、#56、#57 周等,而这些周没有存在。

这里是 HTML 的摘录(结构在几周内重复,内容当然发生变化):

<div class="quotes-container quote-53">
 <div class="quote">Quote here...</div>
 <div class="author">&mdash; 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>

知道发生了什么吗?

非常感谢您对此的任何帮助。

A while ago I needed a script to update some content every week, and my question was answered in this forum.

--

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.

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.

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:

<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>

Any idea what's going on?

Thanks a lot for any help on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-10-16 18:37:37

编辑:您使用的脚本将“周”视为星期日至星期六。由于一年并不总是从星期日开始,因此它会将第一个部分周视为第一周。

如果您只想从一年的第一天开始计算 7 个周期,请使用下面的脚本。如果您想要基于星期日,那么您使用的脚本是正确的。


还有一周#53。这还不是完整的一周。

但现在已经是 52 周了。 我猜当前的脚本没有考虑闰年。

相反,您可以计算今年的日期,并将其除以 7。

示例: http://jsfiddle.net/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< 的结果/code> 一年的最后一天(或闰年的最后两天)。

编辑:修复了 dayOfYear1 的偏移量。

EDIT: 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.

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.


There is a week #53. It's just not a full week.

But we are in week 52. I'm guessing the current script doesn't account for leap years.

Instead you can calculate the day of this year, and divide that by 7.

Example: http://jsfiddle.net/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>

You'll still get a result of 53 on the last day of the year (or last two days in a leap year).

EDIT: Fixed a offset of 1 on the dayOfYear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文