当我在输入字段中提到颜色并点击按钮时如何更改背景颜色

How to change background color when I mention color on input field and hit button

好的,我有一个简单的 formform,它有一个输入字段 id #input。我有一个具有添加值的按钮。现在我的动机是,当我在 input 字段上键入任何颜色并在 click 添加 button 时,该颜色应该显示在 body.

的背景上

你可以这样做:

<form>
    <input type="text" id="input">
    <button onclick="document.body.style['background-color'] = document.getElementById('input').value;return false;">add</button>
</form>

使用事件侦听器实现相同目标:

document.getElementById("mybutton").addEventListener("click", function(event) {
    document.body.style['background-color'] = document.getElementById('input').value;
    event.stopPropagation();
});
    <form>
        <input type="text" id="input">
        <button id="mybutton" type="button">add</button>
    </form>