我想在单击按钮时更改第2行的背景颜色,但唯一改变的是整个背景.
function changeColor() {
document.body.style.backgroundColor = "black";
document.getElementByID("Div2").style.backgroundColor = "black";
}Run Code Online (Sandbox Code Playgroud)
<div id="Div2">
<div>Line 1</div>
<div2 class="line">Line 2</div2>
<div>Line 3</div>
<button onclick="changeColor()">Change color</button>Run Code Online (Sandbox Code Playgroud)