简体   繁体   中英

is there enter and leave event on TextBox - asp.net?

Is there enter and leave event on an asp.net TextBox?

I need that when i click on the TextBox, the TextBox color will be yellow and when I leave the color will be white.

How can I do this?

Use jQuery to handle this, there are two events that you need. onfocus and onblur.

<script type="text/javascript">
$(document).ready(function () {
    $('input').focus(function () {
        $(this).addClass('yellowBackground');
    });
    $('input').blur(function () {
        $(this).removeClass('yellowBackground');
    });
});

Here is the CSS incase you were wondering:

<style type="text/css">
.yellowBackground 
{
    background:yellow;
}
</style>

Edit

The keyword "event" in your question made me originally suggest using javascript, but this can be achieved in CSS using the :focus selector: input:focus { background-color: yellow }

https://jsfiddle.net/3dLp0f03/

You need to use Javascript for that.
Unlike the Click (ASP.NET) event, which can be handled on the server, mouse enter and leave events are handled on the client side, via a script language such as Javascript.

If you want to have considerable backwards compatibility in this regard, you will need to react to the onfocus and onblur events of the input element produced by the Textbox control.

However, since it's not likely to be vital to have it work on older browsers (being a decorative matter) it can be done more easily simply with the CSS:

input:focus { background: yellow; }

One can easily extend this further to only apply to some such elements.

Have a look at onblur and onfocus

onblur Event

The onblur event occurs when an object loses focus.

onfocus Event

The onfocus event occurs when an object gets focus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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