【问题标题】:how to change color code in javascript如何在javascript中更改颜色代码
【发布时间】:2018-03-15 22:36:55
【问题描述】:

大家好,我尝试从颜色词$("#Tcounter").css("color","black") 输入颜色代码;在这个脚本中从"black""#317D29"。我怎样才能做到这一点?

<script type="text/javascript">
$(document).ready(function()  {
    var Tcharacters = <?php echo $max_character_length_title; ?>;
    $("#Tcounter").append("<small><?php osc_esc_js(_e('You have','ctg_housing')); ?> <strong>"+  Tcharacters+"</strong> <?php osc_esc_js(_e('characters remaining','ctg_housing')); ?></small>");
    $("#title<?php echo osc_current_user_locale(); ?>").keyup(function(){
    if($(this).val().length > Tcharacters){
        $(this).val($(this).val().substr(0, Tcharacters));
        }
    var Tremaining = Tcharacters -  $(this).val().length;
    $("#Tcounter").html("<small><?php osc_esc_js(_e('You have','ctg_housing')); ?> <strong>"+  Tremaining+"</strong> <?php osc_esc_js(_e('characters remaining','ctg_housing')); ?></small>");
    if(Tremaining <= 10)
    {
        $("#Tcounter").css("color","red");
    }
    else
    {
        $("#Tcounter").css("color","black");
    }
});
</script> 

谢谢

【问题讨论】:

  • 还有……有什么问题?只需输入 #317D29 而不是 black
  • @Ansel Mae 为什么$("#Tcounter").css("color","#317D29") 不起作用?
  • 不,那是行不通的
  • 但是当它只是黑色时它可以工作?
  • 您上面的脚本甚至无效

标签: javascript jquery css


【解决方案1】:

首先让我知道 Tcounter 是 id 还是 class。

如果 Tcounter 是类使用:$(".Tcounter").css("color","#ff0000");

如果 Tcounter 是 id 则使用:$("#Tcounter").css("color","#ff0000");

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
                $(".Tcounter").css("color","#ff0000");
     			$("#Tcounter").css("color","#ADFF2F");
    });
});
</script>
</head>
<body>
<p class="Tcounter">Red Color using class</p>
<p id="Tcounter" >Green Color using Id</p>
<button >Set background-color of p</button>
</body>
</html>

【讨论】:

  • w3schools.com/cssref/css_colors.asp 参考这个,因为 css 颜色代码使用相同。如果必须添加 jquery 脚本,请也检查该脚本。
【解决方案2】:

如果您想使用十六进制代码,请执行此操作。

$("#Tcounter").css("color","#317D29");

【讨论】:

  • 此脚本将按预期工作。在对答案投票之前确保您的脚本有效。
  • 我发现错误颜色是css文件中的默认值
  • 是的,它现在可以工作了,问题是 css 文件中的主体颜色
猜你喜欢
  • 2012-06-19
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多