我有一个设置了var属性的元素,如下所示:
<div class="divwithbackground" style="--page-header-section-background: url('myurlimage.jpg');">
</div>
CSS
.divwithbackground::after {
background-image: var(--page-header-section-background);
background-position: center;
background-size: cover;
}
这实际上很好。但是,如果我还有另一个设置属性的JavaScript会怎么办:
document.documentElement.style.setProperty('--page-header-section-background',bgDataValue,'important');
这样。好吧,如果我从DIV标签中删除了样式,这将起作用:
style="--page-header-section-background: url('myurlimage.jpg');
但这不是我想要的行为。我想用javascript替换已经设置的属性。这可能吗?