【发布时间】:2019-01-09 12:22:51
【问题描述】:
body.style.background = 在 script.js 包含的文件中不起作用 其他一切都起作用 兑现的 dom 选择器起作用,键入 body.style.background = 'red';有效,但不在包含的文件中
我已经尝试了 console.logs,如下面的代码中所述,输出奇特的是 如果我从回调中复制日志并将其粘贴到 body.style.background 它可以工作但不在脚本文件中。
color_1 = document.querySelector("#color-1");
color_2 = document.querySelector("#color-2");
body = document.querySelector("body");
function changeBackgroundColor() {
body.style.background = "linear-gradient(to right, "+color_1.value+ ", "+ color_2.value+");";
//************* I am getting the console.logs but the background's not changing
console.log("linear-gradient(to right, " + color_1.value + ", " + color_2.value + ");");
}
//************* Both the listeners work fine and the cashed selectors
color_1.addEventListener("input", changeBackgroundColor);
color_2.addEventListener("input", changeBackgroundColor);
<html>
<head>
<title>Background Color Generator</title>
<!-- Custom Styles -->
<style type="text/css">
body {
background: linear-gradient(to right, red , yellow);
}
</style>
</head>
<body>
<div class="container">
<input id="color-1" type="color">
<input id="color-2" type="color">
</div>
</body>
</html>
当颜色输入改变但没有任何反应时,背景应该改变颜色。
【问题讨论】:
标签: javascript dom background linear-gradients