简体   繁体   English

专注于文本字段,背景不改变颜色

[英]focus on a textfield the background is not changing colour

Hi this may be a silly question but when i click on a textfield i want the back ground to change colour but i do not see why it is not working 嗨,这可能是一个愚蠢的问题,但是当我单击文本字段时,我想让背景改变颜色,但是我不明白为什么它不起作用

could someone tell me where i am going wrong 谁能告诉我我要去哪里错了

code is below 代码在下面

//JQUERY
$(function() {

    $('input[type="text"]').focus(function() 
    {
        $(this).addClass("focus");
    });

    $('input[type="text"]').blur(function() 
    {
        $(this).removeClass("focus");
    });

})

; ;

//CSS
.focus {
    border: 2px solid #AA88FF;
    background-color: #FFEEAA;
}

HTML //All the textfield are type text when i inspect them too HTML //当我也检查它们时,所有type text字段均为type text

You can do this in CSS with no need for jQuery (for the newer major browsers at least) 您可以在CSS中完成此操作,而无需jQuery(至少对于较新的主流浏览器而言)

input[type="text"]:focus
{
   border: 2px solid #AA88FF;
   background-color: #FFEEAA;
}

:) :)

Seems like a quirk of the jquery library. 好像是jquery库的怪癖。 Try: 尝试:

$(this).css({...})

instead of: 代替:

$(this).addClass(...)

and it should work. 它应该工作。

I'm not sure why you're using jQuery here, its perfectly possible to change background colours with CSS. 我不确定您为什么在这里使用jQuery,使用CSS更改背景颜色完全有可能。

    input[type=text]:focus {
        border: 2px solid #AA88FF;
        background-color: #FFEEAA;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM