gpt4 book ai didi

javascript - 更改 html 标签本身的背景

转载 作者:行者123 更新时间:2023-11-30 09:50:46 24 4
gpt4 key购买 nike

如何改变这个标签的背景?

HTML

<!DOCTYPE html>
<html class="full" lang="en">

JS

    window.onload = function () {
var fileNumber = Math.floor(Math.random() * (9 - 1 + 1)) + 1;
var imgContainer = document.getElementsByClassName("full");

// It does not work...
imgContainer.style.background = "url('../images/' + fileNumber + '.jpg') no-repeat center center fixed";
}

CSS

.full {
background: url('../images/5.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}

最佳答案

document.getElementsByClassName 返回一个数组,因此您需要使用:

imgContainer[0].style.background = "url('../images/" + fileNumber + ".jpg') no-repeat center center fixed";

关于javascript - 更改 html 标签本身的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655669/

24 4 0