享受你的(日)消息的其余部分

时间:2015-11-22 08:29:29

标签: jquery html5

我一直在使用getDay()和HTML5数据属性来尝试显示类似于Basecamp页脚的消息。

"享受你周日的剩余时间!" https://basecamp.com/

我设法使用jquery返回星期几,但不知道如何将其应用于数据属性。

会喜欢一些帮助。感谢。

1 个答案:

答案 0 :(得分:2)

假设你在html代码中有这样的东西

<p>Enjoy the rest of your <span data-day="wday"></span>!</p>

像这样定义一个javascript函数:

function dayOfWeekAsString(idx) {
      return ["Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][idx];
    }

然后你可以做

var d=new Date();
var day_idx = d.getDay();
var week_day = dayOfWeekAsString(day_idx);
$("p").find("[data-day='wday']").html(week_day);

编辑:使用Jsfiddle链接here

更新了代码

&#13;
&#13;
function dayOfWeekAsString(idx) {
  return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][idx];
}


var d = new Date();
var day_idx = d.getDay();
var week_day = dayOfWeekAsString(day_idx);
$("p").find("[data-day='wday']").html(week_day);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Enjoy the rest of your <span data-day="wday"></span>!</p>
&#13;
&#13;
&#13;