【问题标题】:How to change the background-color of an input field based on the val of that input with jquery如何使用 jquery 根据输入的 val 更改输入字段的背景颜色
【发布时间】:2021-07-05 12:51:36
【问题描述】:

我有一个带有十六进制颜色值的输入字段:

<input class="color" value="#ff0000" /> // ff0000 is red 

如何从该字段读取 val 并将该值设置为该字段的背景颜色? 所以我想实现:

<input class="color" style="background-color: #ff0000;" value="#ff0000" />

【问题讨论】:

  • 当您标记 jQuery 时,请研究 api.jquery.com/onapi.jquery.com/css。如果您自己尝试实现此目的,请将代码添加到问题中,以便我们帮助您调试它。

标签: javascript jquery background-color inline-styles


【解决方案1】:

一点帮助:

$(document).ready(function(){
   var bgcolor = $('.color').val(); // create var which reads the value from input with class .color
   $('.color').css( "background-color", bgcolor ); // now set css rule to the same .color class with that var
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2018-03-24
    • 2013-12-07
    • 2021-03-07
    • 2021-08-29
    • 2011-11-28
    相关资源
    最近更新 更多