Mouse in the search box when you click on the text disappears, often use the search box to get focus and lost focus on the event, next introduce the specific code, interested in the number of friends can refer to the next
input loses focus and gets focus
The text disappears when the mouse clicks in the search box.
When we do the site often use the search box to get focus and lose focus of the event, because lazy, every time to write very annoying, so once and for all, encounter similar situation to call on the OK
Related JS code:
Copy CodeThe code is as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>input loses focus and gets focus jquery Focus Event Plugin-lazy people build station </title>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type= "Text/javascript" ></ Script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
Focusblur
Jquery.focusblur = function (focusid) {
var Focusblurid = $ (FOCUSID);
var defval = Focusblurid.val ();
Focusblurid.focus (function () {
var Thisval = $ (this). Val ();
if (thisval==defval) {
$ (this). Val ("");
}
});
Focusblurid.blur (function () {
var Thisval = $ (this). Val ();
if (thisval== "") {
$ (this). Val (Defval);
}
});
};
/* Below is the calling method */
$.focusblur ("#searchkey");
});
</script>
<body>
<form action= "" method= "POST" >
<input name= "" type= "text" value= "Enter search keywords" id= "Searchkey"/>
<input name= "" type= "Submit" id= "searchbtn" value= "Search"/>
</form>
<p>input loses focus and gets focus jquery Focus event plug-in, <br/><strong style= "color: #F00" > Mouse click in the search box when the text disappears </ Strong>. </p>
</body>
jquery gets and loses focus events
Copy CodeThe code is as follows:
<script src= "Jquery-1.9.1.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (' #username '). focus (function ()//time that is triggered when the classroom is received
{
$ (' #username '). Val (');
})
$ (' #username '). blur () The time that is triggered when function () loses focus
{
if ($ (' #username '). val () = = ' Marry ') {
$ (' #q '). Text (' Username already exists! ‘)
}
else {$ (' #q '). Text (' ok! ')}
})
jquery gets focus and loses focus