Home > Article > Web Front-end > jQuery events: comparison between bind method and blur() method
1.bind method
bind() method adds one or more to the selected elements Event handling procedures, and functions that run when an event occurs.
Since jq version 1.7, the on() method is the preferred method to add event handlers to the selected element
$(selector).bind(event,data,function,map)
Parameters | Description |
---|---|
##event | Required. Specifies one or more events to be added to the element.
Separate multiple event values by spaces. Must be a valid event. |
data | Optional. Specifies additional data to be passed to the function.|
function | Required. Specifies a function to run when an event occurs.|
map | Specifies event mapping ({event:function, event:function, ...}), Contains one or more events to be added to the element, and a function to run when the event occurs. |
参数 | 描述 |
---|---|
function | 可选。规定当 blur 事件发生时运行的函数。 |
例1、添加函数到blur事件,当input字段失去焦点时发生blur事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> 添加函数到blur事件 </title> <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").blur(function(){ alert("输入框失去了焦点"); }); }); </script> </head> <body>
e388a4556c0f65e1904146cc1a846bee在输入框写些东西,然后点击输入框外,让其失去焦点。94b3e26ee717c64999d7867364b1b4a3
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
例2、填写表单时需要实现如下效果
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>鼠标点击后无文字,挪开鼠标后有文字</title> <script language="JavaScript"src="../jQuery/jquery-1.7.1.min.js"></script> <script type="text/javascript"> $(function(){ /*进入焦点时触发*/ $("#account").focus(function(){ varoldValue = $(this).val(); if(oldValue == this.defaultValue){ $(this).val(""); } }); /*失去焦点时触发*/ $("#account").blur(function(){ alert("12"); varoldValue = $(this).val(); if(oldValue == ""){ $(this).val(this.defaultValue); } }); }); </script> </head> <body> 帐号:<input id="account"name="account" type="text" value="请输入帐号"> </body> </html>
The above is the detailed content of jQuery events: comparison between bind method and blur() method. For more information, please follow other related articles on the PHP Chinese website!