【发布时间】:2019-06-15 00:33:59
【问题描述】:
我正在尝试在按下按钮时更改 css 类的“背景图像:”。
jQuery:
$(function() {
$('button').on('click', function() {
$('.masthead2').css('background-image', 'url("../img/whiteheader.png")');
});
});
CSS:
.masthead2 {
position: relative;
width: 100%;
height: auto;
min-height: 35rem;
padding: 15rem 0;
background-image: url("../img/roseheader.png");
background-position: center;
background-repeat: no-repeat;
background-attachment: scroll;
background-size: cover;
}
当我按下按钮时,CSS 样式中的“背景图像:”被删除。然后 HTML 从此改变:
<header class="masthead2">
到这里:
<header class="masthead2" style='background-image: url("../img/whiteheader.png");'
但是,在页面上;根本没有图像显示。我尝试将 url 直接复制到 css 中并加载新图像。所以我相信这个网址是正确的。
如果直接在css中更改背景图像,这应该可以解决问题。
【问题讨论】:
-
你检查过控制台吗?
-
@Adrift 是的,没有任何显示:/
标签: javascript jquery html css