如何在两个图像之间切换作为我的body背景?



我想做一个按钮,改变我的HTML页面的背景。遗憾的是,我所尝试的一切都没有成功,我的功能只是不做任何点击。我尝试了一些其他人贴在这里的东西,但没有一个有效。

function night() {
let element = document.body;
if (element.src.match('image/night.jfif')) {
element.src = "image/day.jpg";
console.log("day")
} else {
body.style.backgroundimage = "url('image/night.jfif')";
console.log("night")
}
}
<body style="background-image:url('image/Day.jpg')">
<button onclick="night()"> Theme </button>
</body>

function night() {
if (currentImage === 'image1.jpg') {
document.body.style.backgroundImage = 'url("image2.jpg")';
currentImage = 'image2.jpg';
} else {
document.body.style.backgroundImage = 'url("image1.jpg")';
currentImage = 'image1.jpg';
}
}

试试这个。这可能会有帮助

可以了

var backimg = 0;
function night(){
backimg = 1-backimg;
let b = 'image/Day.jpg';
if (backimg == 1){
b = 'image/night.jfif';
}

document.body.style.backgroundImage = "url('"+b+"')";

}
<body style="background-image:url('image/Day.jpg')">
<button onclick="night()"> Theme </button>
</body>

最新更新