3 回答

TA貢獻1784條經驗 獲得超2個贊
您正在嘗試將src
屬性設置為 on<div id="slc_on_label">
而不是在<img>
其內部。
您可以使用document.querySelector()
選擇里面的圖像div
:
document.querySelector("#slc_on_label img").src = "https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg";

TA貢獻1875條經驗 獲得超5個贊
<html>
<head>
<style>
#slc_on_label img {
position: fixed;
width: 5.5%;
left: 10%;
top: 10%;
}
.button {
background-color: Powderblue;
font-size: 5vw;
}
</style>
</head>
<body>
<div id="slc_on_label">
<img src="https://ichef.bbci.co.uk/news/800/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg">
</div>
<label class="button" id="button" onclick="run(this)">0</label>
<script>
function run(button) {
document.querySelector("#slc_on_label img").setAttribute('src', 'https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg');
button.style = "background-color:red";
};
</script>
</body>
</html>

TA貢獻1820條經驗 獲得超2個贊
function run(button) {
document.querySelector('slc_on_label img').src =
'https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg';
button.style.backgroundColor = 'red';
}
也不破壞按鈕樣式。
添加回答
舉報