【问题标题】:jquery change font colorjquery改变字体颜色
【发布时间】:2014-01-16 18:19:53
【问题描述】:

如何根据 jquery 中的输入值更改字体颜色?

这是我的代码

<h3 id="quoteDiv"></h3>

 <input id="input1" type="text" onfocus="if(this.value == 'Valfri spelare') { this.value = ''; }" value="Valfri spelare"/>

 var quotes = ["(tjej1) exampletext!",];


var user1 = document.getElementById("input1").value;


//Sub in the usernames
                    var ind = Math.floor(Math.random() * quotes.length); //select random quote
                    var newquote = quotes[ind].replace("(tjej1)", user1);


var quotediv = document.getElementById("quoteDiv"); //get the div (or any other element) that you want to display the quote in
                    quotediv.innerHTML = newquote; //display your quote in the div.

如何将值的颜色从 input1 (tjej1) 更改为绿色而不是标准的 h3?我只希望值改变颜色:)

谢谢!

【问题讨论】:

    标签: jquery input colors quotes


    【解决方案1】:
    var newquote = quotes[ind].replace("(tjej1)", '<span style="color:green">'+user1+'</span>');
    

    【讨论】:

    • 我应该在哪里写?:O
    • 这不会改变引号中的字体颜色:(
    • 我希望颜色在

      而不是在输入中改变:(
    • $('#input').chnage(function(){ $('#quoteDiv').css('background-color','green'); });
    【解决方案2】:

    您可以通过 CSS 设置 h3 样式:

    #quoteDiv{
        color: green;
    }
    

    【讨论】:

    • 我只希望值改变颜色
    【解决方案3】:

    jquery 键位

    $('#input').keydown(function(){
    
       $(this).css('color','green');
    
    });
    

    Jquery 键位

    $('#input').keyup(function(){
    
       $(this).css('color','green');
    
    });
    

    jquery 修改

    $('#input').chnage(function(){
    
       $(this).css('color','green');
    
    });
    

    【讨论】:

    • jsfiddle.net/2ufnK/4 我想把输入的字体颜色改成绿色
    • 你什么时候愿意改?
    • jsfiddle.net/2ufnK/4 检查这个并尝试更改quotediv中的值字体颜色,只有值
    • 不只是输入的值应该是绿色的,而不是整个引用
    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 2021-09-30
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2017-03-22
    • 2018-04-04
    • 2014-10-10
    相关资源
    最近更新 更多