如何在onfocus时更改文本框的背景颜色? [英] How do i change Background color of textbox when onfocus?

查看:607
本文介绍了如何在onfocus时更改文本框的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这个代码,但它不工作..当我的焦点文本框它显示错误

i tried this code but its not working.. when i on-focus textbox it shows error

   function ChangeBgColor(obj, evt) 
    { 
            if(evt.type=="focus") 
                style.background ="lightgrey";
            else if(evt.type=="blur") 
            {
               style.background="white";
            }          
    }


推荐答案

style ?您尚未在上述代码中的任何位置定义它:

What is style? You have not defined it anywhere in your code above:

function ChangeBgColor(obj, evt) 
{ 
    if(evt.type=="focus") 
        style.background ="lightgrey";  //<--what is style?
    else if(evt.type=="blur") 
    {
        style.background="white";
    }          
}

我猜你想要像

obj.style.background ="lightgrey";

在上面的代码中,简化了

And in the code above, simplified

function ChangeBgColor(obj, evt) 
{ 
    obj.style.background = (evt.type=="focus") ? "lightgrey" : "white";
}

最好的答案是使用纯CSS。

The best answer is to use pure CSS.

这篇关于如何在onfocus时更改文本框的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆