duanche4578 2015-11-07 22:28
浏览 11
已采纳

颜色处理程序改变CSS

I am trying to make it possible to change the background color of pull quotes by using the input type="color" tag in a form. my HTML is as follows

   <form action="change.php" method="post">
    name:  <input type="text"><br/>
    email: <input type="email" id="email"><br/>
     <label for="background-color">Choose a color for background :</label> 
    color: <input type="color" name="bgcolor"><br/>
    <input type="submit" name="submit" value="Submit">
    </form>

You see I was trying to do it in PHP by getting the color value from a POST , but I would reay like to solve this question in Jquery if possible.

  • 写回答

1条回答 默认 最新

  • dou448172583 2015-11-07 22:35
    关注

    You can achieve this with something like the following. You need an id attribute for you input. Then on blur event you will change the div background color with the input value if it is valid.

    Also, you have to include jQuery in html <head>.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    

    JSFIDDLE

    HTML:

    <input type="text" name="bgcolor" id="bgcolor" maxlength="6" placeholder="Hex representation of the color">
    

    JQuery:

    $(document).ready(function() {
        $('#bgcolor').on('change', function() {
            var color = $(this).val();
            var isOk  = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test('#'+color)
            if (isOk) { //If input is a valid representation of a color
                $('#your_div_id').css('background-color', '#'+color);
            }
        });
    });
    

    isOk regex from here.



    Or using type="color":

    HTML:

    <input type="color" name="bgcolor" id="bgcolor" value="">
    

    JQuery:

    $(document).ready(function() {
        $('#bgcolor').on('change', function() {
            var color = $(this).val();
            $('#your_div_id').css('background-color', color);
        });
    });
    



    UPDATE:

    Move the following inside $(document).ready(function () { like this:

    $(document).ready(function () {
    
        $('span.pq').each(function () {
            var quote = $(this).clone();
            quote.removeClass('pq');
            quote.addClass('pullquote');
            $(this).before(quote);
        }); //end each
    
        $('#note').draggable();
    
        $('#bgcolor1').on('change', function () {
            var color = $(this).val();
            $('#id1').css('background-color', color);
        });
    
        $('#bgcolor2').on('change', function () {
            var color = $(this).val();
            var isOk = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test('#' + color)
            if (isOk) { //If input is a valid representation of a color
                $('#id1').css('background-color', '#' + color);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c