How do i change Background color of textbox when onfocus?

后端 未结 5 1947
栀梦
栀梦 2021-01-16 05:31

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

   function ChangeBgColor(obj, evt) 
    { 
            if(evt.type==\"focus\"         


        
5条回答
  •  忘掉有多难
    2021-01-16 06:14

    JavaScript is not necessary for this task, just use css (:focus is supported since IE8)

    https://developer.mozilla.org/en-US/docs/CSS/:focus

    input { background: lightgray; }
    input:focus { background: white; }
    

    Only if this effect is absolutely needed also on < IE8 then JS (properly wrapped in a conditional comment) can be used, but even in this scenario it is recommended to keep off style from logic: e.g.

    function ChangeBgColor(obj, evt) { 
        obj.className = (evt.type === "focus") ? "focus" : "";
    }
    

    and use this style

     input { background: lightgray; }
    
     input:focus, 
     input.focus { background: white; }
    

提交回复
热议问题