【问题标题】:How to change text color depending on the stat of the element (focus,selected,hover)如何根据元素的状态更改文本颜色(焦点、选定、悬停)
【发布时间】:2012-05-11 19:04:33
【问题描述】:

我想在输入元素被聚焦后改变它的颜色,原来的颜色是灰色,当我悬停它变成蓝色,和我聚焦时一样,蓝色,在我检查表单是否有效之后,当不会写电子邮件,有一个 jquery 函数会将背景颜色更改为红色,颜色更改为白色并将值更改为一些警报消息,但是当用户再次单击输入时,该背景颜色将返回到白色,我希望颜色恢复正常,灰色 - 蓝色(悬停,选中或聚焦)

$("#email").focus ( function() { if(document.getElementById('email').style.backgroundColor=="Crimson") { $("#email").css("background-color","white");
                                            $("#email").css("color","grey");
                                                $("#email").css("font-weight","normal");
                                                $("#email").val(""); }});

【问题讨论】:

  • 不清楚你想做什么,你能不能试着组织你的问题并明确定义你的问题。

标签: jquery colors focus hover selected


【解决方案1】:

css('background-color')rgb(r, g, b) 格式返回颜色,因此如果您将其设置为Crimson,那么此代码应该可以解决您的问题。

$("#email").focus(function() {
    if ($('#email').css('background-color') == "rgb(220, 20, 60)") {
        $("#email").css("background-color", "white");
        $("#email").css("color", "grey");
        $("#email").css("font-weight", "normal");
        $("#email").val("");
    }
});

【讨论】:

    猜你喜欢
    • 2013-10-12
    • 2021-01-20
    • 1970-01-01
    • 2018-11-30
    • 2021-10-31
    • 1970-01-01
    • 2017-07-03
    • 2021-04-18
    相关资源
    最近更新 更多