javascript - 在 dytime 基础上更改 div 背景

标签 javascript html css background-image dst

我在特定日期更改超大屏幕背景时遇到问题,但没有任何显示,而且超大屏幕没有任何图片作为背景。

我的 Js:

var currentTime = new Date().getHours();
if (7 <= currentTime && currentTime < 20) {
    if (document.getElementById("jumbotron")) {
        document.getElementById("jumbotron").background = "1.jpg";
    }
}else {
    if (document.getElementById("jumbotron")) {
        document.getElementById("jumbotron").background = "2.jpg";
    }
}

最佳答案

您需要使用正确的语法:

document.getElementById("jumbotron").style.backgroundImage = "url('1.jpg')";

http://www.w3schools.com/jsref/prop_style_backgroundimage.asp

完整示例在这里:

https://jsfiddle.net/gevorgha/15baqpjt/

关于javascript - 在 dytime 基础上更改 div 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37374676/

相关文章:

css - select, submit 元素有 20px 右边距

css - css3 是否可以垂直文本溢出?

javascript - Angularjs:正确设置日期时间本地,但提交 null

javascript - 如何使用javascript浏览器端获取显卡驱动程序名称?

css - 如何将我的 Logo 置于我的移动网站的中心?

javascript - CSS - 在关键帧中触发悬停

html - 无法将 Logo 图像的属性设置为响应

javascript - 有什么办法可以阻止 Chrome 开发者工具中的 div 滑动?

javascript - FabricJS:在 Canvas 上使用对象控件来调整大小,但不能缩放

javascript - 当悬停被移除或消失时如何应用过渡?