Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何使用输入类型=颜色更改背景颜色?如果我选择蓝色,则背景颜色为蓝色,如果我选择红色,则背景颜色为红色。
监听input事件并将输入的值赋给backgroundColorbody 样式的属性:
input
backgroundColor
document.querySelector('input').addEventListener('input', function(){ document.body.style.backgroundColor = this.value; })
<input type="color">