我正在尝试更改div背景颜色,但没有.创建用于更改元素背景颜色的新函数


<html>
<body>

<input type="color" id="inputColor">
<input type="number" placeholder="Enter Number" id="inputDivNumber">
<button onclick="createDiv()">Create Div</button>
<button onclick="changeBackground()">Change Div Background</button>
<div id="output" class="divOutput"></div>
<script>
var div;
function createDiv() {
var i = 0;
var divValue = document.getElementById("inputDivNumber").value;
while (i < divValue) {
div = document.createElement("div");
//div.innerHtml=i;
i++;
div.setAttribute('class', 'divclass');
document.getElementById("output").appendChild(div);
}
}
function changeBackground() {


document.getElementsByClassName('.divclass').style.backgroundColor);
}

</script>
</body>
</html>

要生成更改背景颜色的函数,u可以执行以下操作:

function changeBackgroundColor(element, color) {
element.style.backgroundColor = color;
}

请下次在发帖前自行搜索并解释的问题

以下是您修复的代码:https://codepen.io/LENNY74/pen/ZEKzpNo

最新更新