惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
The Cloudflare Blog
G
Google Developers Blog
博客园_首页
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
D
Docker
C
Check Point Blog
T
Tailwind CSS Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - xpengfee

【转】Quartz.NET 【转】阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访 【转】关于RabbitMQ 【转】windows下nginx+mono+fastCGI部署asp.net网站 【转】玩玩负载均衡---在window与linux下配置nginx 【转】亿级Web系统搭建——单机到分布式集群 【转】集群和负载均衡的概念 【转】SQL Server 2008 新数据类型 Dokuwiki 【转】反向AJAX 【转】Asp.net MVC Comet推送 【转】Comet:基于 HTTP 长连接的“服务器推”技术 【转】WEB前端调优 【转】Google Chrome浏览器调试 【转】ASP.NET MVC 4 RC的JS/CSS打包压缩功能 【转】SQLServer连接字符串配置:MultipleActiveResultSets VS妙用--自动创建文件注释头 Post流提交、接收 原型设计工具Axure RP分享
【转】jquery 注册事件的方法
xpengfee · 2015-04-22 · via 博客园 - xpengfee

原文链接:http://outofmemory.cn/code-snippet/2123/jquery-zhuce-event-method

1.使用事件名来绑定,可用的事件名有

change,click,dblclick,error,focus,focusin,focusout,keydown,keypress,keyup,mousedown,mouseenter,mouseleave,mousemove,mouseout,mouseover,mouseup,resize,scroll,select,submit,unload

例如:

$('#target').click(function(){
alert('#target元素绑定了click事件');
});

2.使用bind方法来绑定事件 bind(type,[data],fn) 。type 参数及为我们上面讲的各种事件名,例如:

当每个段落被点击的时候,弹出其文本。

3.使用on方法来注册事件 ,on方法基本和bind方法差不多,其实bind方法最好也是调用on方法来实现的

$('.scv').on('click', function(){
$(this).clone(true).appendTo('#container');
});

4.即使是后来加进来也有效的方法live方法

在老的jQuery版本中,我们有一个方法专门用来处理动态生成的元素的事件绑定 - live(),使用live()方法可以将方法绑定的效果应用到已存在或者新创建的DOM元素。代码如下:

$('.scv').live('click', function(){
$(this).clone().appendTo('#container');
});

5.一次性的事件绑定方法one方法,为每一个匹配元素的特定事件(像click)绑定一个一次性的事件处理函数。当所有段落被第一次点击的时候,显示所有其文本。

$("p").one("click", function(){
alert( $(this).text() );
});