$(selector).focus()
$(selector).focus(function)
参数 | 说明 |
function | 这是一个可选参数。用于指定当元素获得焦点时要运行的函数。 |
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>focus demo</title>
<style>
span {
display: none;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p><input type="text"> <span>Focus starts.. Write your name.</span></p>
<p><input type="password"> <span>Focus starts.. Write your password.</span></p>
<script>
$("input").focus(function() {
$( this ).next("span").css("display", "inline").fadeOut( 2000 );
});
</script>
</body>
</html>
Focus starts.. Write your name.
Focus starts.. Write your password.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>focus demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p><input type="text" value="you can't write"></p>
<p><input type="password"> </p>
<script>
$("input[type=text]").focus(function() {
$( this ).blur();
});
</script>
</body>
</html>