jQuery events: comparison between bind method and blur() method

黄舟
Release: 2017-06-26 14:10:54
Original
1985 people have browsed it

1.bind method

Definition and usage

bind() method adds one or moreto the selected elements Event handlingprocedures, 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


Syntax

$(selector).bind(event,data,function,map)
Copy after login

例1、给

    给<p>标签添加一个单击事件 

点我!

Copy after login


例2、向元素添加多个事件。(添加多个事件)

     向元素添加多个事件     

将鼠标移动到该段落上。

Copy after login

注意:toggleClass() 方法对添加和移除被选元素的一个或多个类进行切换。如果不存在指定的类则添加类,如果已设置则删除之。

例3、使用事件映射来向被选元素添加一些事件/函数(使用事件映射)

     使用事件映射来向被选元素添加一些事件/函数    

这是一个段落。

Copy after login


例4、向一个自定义命名的事件处理程序传递数据。(向函数传递数据)

     向一个自定义命名的事件处理程序传递数据    

点我!

Copy after login

2、blur()方法

定义和用法

当元素失去焦点时发生 blur 事件。

blur() 方法触发 blur 事件,或规定当发生 blur 事件时运行的函数。

提示:该方法常与 focus() 方法一起使用。


语法

为被选元素触发 blur 事件:

$(selector).blur()
Copy after login

添加函数到 blur 事件:

$(selector).blur(function)
Copy after login
Required. Specifies one or more events to be added to the element. Optional. Specifies additional data to be passed to the function. Required. Specifies a function to run when an event occurs. Specifies event mapping (

标签添加一个单击事件

Parameters Description
##event
Separate multiple event values by spaces. Must be a valid event.
data
function
map{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事件

     添加函数到blur事件    
Copy after login

在输入框写些东西,然后点击输入框外,让其失去焦点。




例2、填写表单时需要实现如下效果

    鼠标点击后无文字,挪开鼠标后有文字   
帐号:
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!