ドキュメント

Events/focus

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Events

focus( )

Triggers the focus event of each matched element.
This causes all of the functions that have been bound to the focus event to be executed. Note that this does not execute the focus method of the underlying elements.
Examples:

To focus on a login input box with id 'login' on page startup, try:

$(document).ready(function(){
  $("#login").focus();
});

NameType

focus( fn )

Binds a function to the focus event of each matched element.
The focus event fires when an element receives focus either via the pointing device or by tab navigation.
Arguments:
fnFunction
A function to bind to the focus event on each of the matched elements.
function callback(eventObject) {  
this; // dom element
}
Examples:

Fire focus.

    $("input").focus(function () {
         $(this).next("span").css('display','inline').fadeOut(1000);
    });

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("input").focus(function () {
         $(this).next("span").css('display','inline').fadeOut(1000);
    });

  });
  </script>
  <style>span {display:none;}</style>
</head>
<body>
  <p><input type="text" /> <span>focus fire</span></p>
<p><input type="password" /> <span>focus fire</span></p>
</body>
</html>

To stop people from writing in text input boxes, try:

$("input[type=text]").focus(function(){
  $(this).blur();
});

NameType