Border color changes when the focus of the text box in JavaScript

Source: Internet
Author: User


Example

The code is as follows: Copy code

Function appendit ()
  { 
Var nodes = document. getElementsByTagName ("INPUT ");
For (var I = 0; I <nodes. length; I ++)
  { 
Var ctype = nodes [I]. getAttribute ("type ");
If (ctype = 'text ')
  { 
Nodes [I]. onfocus = function () {this. style. backgroundColor = '#33ff00 ';}
Nodes [I]. onblur = function () {this. style. backgroundColor = '# 3366FF ';}
  } 
  } 
  } 

Test:

<Input type = "text" size = "36" name = "title"/>


Let's look at another example.

Click the border color-changing text box. When you click the text box to be entered, the text box will automatically color-changing and highlighted, which has a very visual effect, making the text box much more beautiful.

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "textml; charset = utf-8"/>
<Title> border color change when you click in the input box </title>
</Head>
<Body>
<Script type = "text/javascript">
// FocusClass: the style when the focus is obtained
// NormalClass: normal style
Function focusInput (focusClass, normalClass ){
Var elements = document. getElementsByTagName ("input ");
For (var I = 0; I <elements. length; I ++ ){
If (elements [I]. type! = "Button" & elements [I]. type! = "Submit" & elements [I]. type! = "Reset "){
Elements [I]. onfocus = function () {this. className = focusClass ;};
Elements [I]. onblur = function () {this. className = normalClass | '';};
        }
    }
}
</Script>
<Script type = "text/javascript">
Window. onload = function (){
FocusInput ('focusinput', 'normalinput ');
}
</Script>
Enter the name: <input type = "text" class = "normalInput"/>
<Style type = "text/css">
. NormalInput {border: 1px solid # ccc ;}
. FocusInput {border: 1px solid # FFD42C ;}
</Style>
</Body>
</Html>

It works in Firefox, but Firefox and chrome. By default, these two browsers will automatically input a box to add the Click effect, so sometimes they cannot see clearly, and IE is outstanding.


Introduce the popular jquery-based implementation method as an example.

The code is as follows: Copy code

Html code:

<Div class = "fun">

<H1> text box focus clear default value border color, leave focus add default value

<Div class = "text">

<Input type = "text" class = "inputText" value = "2222"/>

<Input type = "text" class = "inputText" value = "2222"/>

<Input type = "text" class = "inputText ss" value = "82747"/>

<Input type = "text" class = "inputText" value = "094727"/>

<Input type = "text" class = "inputText" value = "249049"/>

<Input type = "text" class = "inputText" value = "333"/>

<Input type = "text" class = "inputText" value = "82747"/>

</Div>

</Div>

Css code

:

This is based on the jquery library. Please add it yourself.

The code is as follows: Copy code

<Style type = "text/css">

. Fun {margin: 0 auto; width: 1000px; overflow: hidden; box-shadow: 0 3px 6px rgba (0,0, 0, 0.1); border: solid 1px # ccc; font-family: Microsoft YaHei; overflow: hidden ;}

. InputText {border: solid 1px # ccc; height: 40px; width: 200px; line-height: 40px/9; padding: 0 2px; box-shadow: inset 0 0 4px rgba (0, 0, 0.1); margin: 10px 0; outline: none; font-family: arial; font-weight: 700; text-indent: 5px; color: # 1C1C1C; display: inline-block ;}

. InputText: focus {border: solid 1px # 1398FF; box-shadow: 0 0 5px rgba (0,192,255, 0.4 );}

. Text {padding: 15px 0 15px 75px ;}

H1 {text-align: center; padding: 10px 0; margin: 0; background:-webkit-linear-gradient (# fcfcfc, # f9f9f9) repeat; background: -moz-linear-gradient (# fcfcfc, # f9f9f9) repeat; border-bottom: solid 1px # ccc; font-weight: 400; text-shadow: 1px 1px 3px # fff;}/* Css3 background gradient */

</Style>

Js code:

<Script type = "text/javascript">

// JavaScript Document

$ (Document). ready (function (){

Function input ()

{

// Use each to traverse the text box

$ (". InputText"). each (function (){

Var $ val = this. value; // Save the value of the current text box

Var $ ss = $ (". ss"). val ();

$ (This). focus (function (){

// When the focus is obtained, if the value is the default value, the value of the text box is cleared.

If (this. value = $ val ){

This. value = "";

}

});

$ (This). blur (function (){

// If the value is null when the focus is lost, the default value of the text box is added.

If (this. value = ""){

This. value = $ val;

}

});

});

}

Input ();

})

</Script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.