我正在尝试编写一个函数,该函数background-image
通过单击按钮从 3 个不同的选项中循环,并且代码不起作用。也许有人可以告诉为什么...
function changeBackground (){
console.log('change background');
var b = document.getElementById('mainbody');
var bstyle = window.getComputedStyle(b, null).getPropertyValue('background-image');
if (bstyle == "url('strawberry.png')") {
b.style.backgroundImage = "url('raspberry.png')";
} else if (bstyle == "url('raspberry.png')"){
b.style.backgroundImage = "url('blueberry.png')";
} else {
b.style.backgroundImage = "url('strawberry.png')";
}
}
例如,此更改代码font-size
完美运行。
function changeSize (){
console.log('changing font size');
var s = document.getElementById('clock');
var sstyle = window.getComputedStyle(s, null).getPropertyValue('font-size');
if (sstyle == "25px") {
s.style.fontSize = "50px";
} else{
s.style.fontSize = "25px";
}
}