小编Pus*_*ela的帖子

JavaScript body.style.background 无法在 script.js 中使用线性渐变,但在控制台中工作正常

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);
Run Code Online (Sandbox Code Playgroud)
<html>
  <head>
      <title>Background Color Generator</title>

      <!-- Custom Styles -->
      <style …
Run Code Online (Sandbox Code Playgroud)

javascript dom background linear-gradients

2
推荐指数
1
解决办法
6365
查看次数

标签 统计

background ×1

dom ×1

javascript ×1

linear-gradients ×1