繁体   English   中英

单击GO按钮后,如何将背景色更改为线性渐变,并在输入标签中输入颜色并进行混合?

[英]How to change background color to linear gradient with colors entered in the input tags and mixed after clicking GO button?

我试图将线性梯度放在dom之后,但是它根本没有帮助:(

https://codepen.io/OlegNazarov77/pen/YRjdJX

let firstColor = document.getElementById('colorOne')
let secondColor = document.getElementById('colorTwo')
document.querySelector('button').addEventListener('click',changeColor);

function changeColor(e){
  document.querySelector('body').style.background = linear-gradient(firstColor.value, secondColor.value) ; 

  preventDefault(e);
}

不好意思的代码,我只是在学习。

非常感谢!

我猜是因为:

 linear-gradient(firstColor.value, secondColor.value) ;

不放在'``; 尝试这个:

 document.querySelector('body').style.background = `linear-gradient(${firstColor.value}, ${secondColor.value})`;

第二件事是如果您在代码的前两行中预先分配了值。 firstColorsecondColor的值是不元素值,即产生颜色值任一字符串。 您应该使用这样的东西:

let secondColor = document.getElementById('colorTwo').val;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM