Home  >  Article  >  Web Front-end  >  Methods to gain and lose focus of a text box in js (jquery)_javascript skills

Methods to gain and lose focus of a text box in js (jquery)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:47:151597browse

Let’s first look at the javascript written directly on the input

Copy the code The code is as follows:




jquery implementation method

For the focus event of the element, we can use jQuery's focus functions focus(), blur().

focus(): Used when focus is obtained, the same as onfocus in JavaScript.
For example:
Copy code The code is as follows:

$("p"). focus(); or $("p").focus(fn)

blur(): used when focus is lost, the same as onblur.
For example:
Copy code The code is as follows:

$("p"). blur(); or $("p").blur(fn)

Instance
Copy code The code is as follows:


Here the label covers the text box, which can better control the style




jquery code
Copy code The code is as follows:

$(function() {
$('#searchKey').focus(function() {
$('#lbSearch').text( '');
});
$('#searchKey').blur(function() {
var str = $(this).val();
str = $.trim (str);
if(str == '')
$('#lbSearch').text('Search Shenma?');
});
})

Okay, it’s pretty good
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn