如何使用 JavaScript 更新文本框的背景颜色?

How to update the background color of a textbox using JavaScript?

我想在javascript没有html中制作一个文本框(所以可以在console.log中使用它),用setInterval更新背景颜色。怎么做到的?

这是我目前得到的结果,但没有用。

var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", color);
document.body.appendChild(x);
var color;
setInterval(function(){if (x.setAttribute("value",color)===color{document.body.style.backgroundColor=color};},100);

不要使用 setInterval 来轮询更改,而是使用 keyup 事件来检测用户何时更改值:

var color = '#FFFFFF';
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", color);
document.body.appendChild(x);

x.onkeyup = function(){
    color = this.value;
    document.body.style.backgroundColor = color;
};

Here is a working example

尝试此解决方案与时间间隔一起使用

var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("value", color);
document.body.appendChild(x);
var color = '#FFFFFF';
setInterval(function () {
    color = x.value; // you need to get value of color code 
    document.body.style.backgroundColor = color // then it work here
    console.log(color);
}, 100);