Javascript 在一周中的某一天换班

Javascript 在一周中的某一天换班,javascript,html,css,Javascript,Html,Css,我试图让图像只在一周中的某些日子显示,这样我就可以自动完成这个过程,而不必每天晚上在家里这样做 我在最初建立此网站时提到了此帖子: 现在,我已经将它们的所有样式设置为display:none,并且正在尝试使用JS将一个类应用到一周中适当的一天,然后覆盖内联CSS,将display设置为block。然而,有些东西不起作用 这是在平方空间上,如果相关的话 $('.gene img').eq(new Date().getDay()).addClass('displated') .gene img.d

我试图让图像只在一周中的某些日子显示,这样我就可以自动完成这个过程,而不必每天晚上在家里这样做

我在最初建立此网站时提到了此帖子:

现在,我已经将它们的所有样式设置为display:none,并且正在尝试使用JS将一个类应用到一周中适当的一天,然后覆盖内联CSS,将display设置为block。然而,有些东西不起作用

这是在平方空间上,如果相关的话

$('.gene img').eq(new Date().getDay()).addClass('displated')
.gene img.displated[style]{display:block!important;}

这里有一个简单的JS替代方案。谢谢你的主意

(函数(){
var hrefs=新阵列(7);
hrefs[1]=”https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b6382c74c50461d189c28/1548444556260/1-28.png?format=1000w";
hrefs[2]=”https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b642a0ebbe8fdff7443a5/1548444720987/1-29.png?format=1000w";
hrefs[3]=”https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b63fa8a922d0881f198c0/1548444679119/1-30.png?format=1000w";
hrefs[4]=”https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b63f2c74c50461d18a1e4/1548444668557/?format=1000w";
hrefs[5]=”https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b63e940ec9a53af2de8b6/1548444655398/?format=1000w";
var dayNum=新日期().getDay();
var image=document.createElement(“img”);
if(hrefs中的dayNum){
document.getElementById(“基因”).appendChild(图像);
image.src=hrefs[dayNum]
}
})();

在做这样的事情时,我不太喜欢添加CSS类

我以前使用过这个jQuery解决方案。更新以包含您的“基因”类和“img”类型


$(文档).ready(函数(){
$(“.gene img”).eq(new Date().getDay()).show();
});
编辑以添加其他方法

图像占位符(如果显示一个):此方法在需要之前不会提取图像,而不是使用html提取所有图像,然后在错误的日期不显示图像

<div class="gene">
    <img />

</div>   


我刚刚将其添加到代码块中,但它仍然没有显示出来。squarespace是否可能不支持此特定代码?我已使用不带JQueryTanks的解决方案更新了我的答案!我把它放在我的测试页面上,但不是我真正想要的页面。我有点不知所措。我可能应该学习jquery和javascript。测试页面和实际页面之间有什么区别?你用了什么方法?明白了!只需将jquery/javascript放在代码块中,而不是代码注入。谢谢你,这会帮我省去很多麻烦,很高兴你让它工作了!请接受一个答案,如果与此处的两个答案不同,请提交您自己的答案,然后将其标记为已接受的答案,以让其他人知道问题已解决,并可能在将来帮助其他人
<div class="gene">
    <img />

</div>   
<script>
    $(document).ready(function () {
        var href = new Array(7);

        href[1] = "https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b6382c74c50461d189c28/1548444556260/1-28.png?format=1000w";
        href[2] = "https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b642a0ebbe8fdff7443a5/1548444720987/1-29.png?format=1000w";
        href[3] = "https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b63fa8a922d0881f198c0/1548444679119/1-30.png?format=1000w";
        href[4] = "https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b63f2c74c50461d18a1e4/1548444668557/?format=1000w";
        href[5] = "https://static1.squarespace.com/static/588652c7a5790a5d29f14d94/t/5c4b63e940ec9a53af2de8b6/1548444655398/?format=1000w";
        var day = new Date().getDay();
        if(day in href){
            $(".gene img").attr("src",href[day]);
        } else {
            $(".gene").hide();
        }
    });
</script>