我的代碼有什麼問題?出於某種原因,我的函數load()不會改變.firstDiv背景?背景不改變onclick功能
我找不到任何錯誤。
function load() {
document.getElementsByClassName("firstDiv").style.backgroundImage = "url('https://static.pexels.com/photos/140945/pexels-photo-140945.jpeg')";
}
.firstDiv, .secondDiv {
width: 50%;
height:100vh;
float: left;
}
.firstDiv {
background-image: url("https://static.pexels.com/photos/200303/pexels-photo-200303.jpeg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
box-shadow: inset 0 0 0 1000px rgba(0,0,0,.2);
}
<div class="firstDiv"></div>
<div class="secondDiv">
<button onclick="load()">Change background</button>
</div>
非常感謝幫助。不知道。所以一般情況下最好使用id? – CaL17
這取決於用例。將ID用於多個元素的唯一元素和類。 –