通过输入文本框的键向上更改背景颜色

change background color through onkeyup of input text box

本文关键字:背景 颜色 输入 文本      更新时间:2023-09-26
<input id='ad' onkeyup='sc(id)'/>
<script lang="javascript">
function sc(i)
{
    i.style.backgroundColor="#006400";
}
</script>

这是我用来通过id更改输入框的背景颜色的代码,但它不起作用。请建议一些方法来更改输入框的背景颜色。

如果i是元素 ID,请使用 getElementById()

<input id='ad' onkeyup='sc(id)'/>
<script lang="javascript">
function sc(i)
{
    // Retrieve the element by its id 'i'
    document.getElementById(i).style.backgroundColor="#006400";
}
</script>