jQuery处理事件-上篇

版权声明:版权所有-江西服装学院 https://blog.csdn.net/qq_38263696/article/details/83089403

事件处理程序的简单注册

//单击任意div时,使其背景变成黄色
$('div').click(function(){   $(this).css({backgroundColor:'yellow'});  });
//toggle(),   将多个事件处理程序函数绑定到单击事件, 按顺序一次调用一个函数;
$('div').toggle(function(){this.innerText='0'},function(){this.innerText='1'},function(){this.innerText='2'},);
//hover(), 用来给mouseenter 和 mouseleave事件注册事件处理函数
第一个参数是mouseenterHandler , 第二个参数是mouseleaveHandler,      
如果mouseenterHandler 与mouseleaveHandler相同, 可以合并,只学一个Handler函数

事件处理程序的高级注册

bind();

// 最简单的使用bind方法
 $('div').bind('click','牛逼的bind()',function(event){this.innerText = event.data});

注销事件处理程序

unbind()
$(’*’).unbind() ; //从所有元素中移除所有的jQuery事件处理程序

触发事件

trigger();

自定义事件

//用户单击div , 广播一个自定义事件what事件;
$('div').bind('what',function(event){console.log(event.type)});
$('div').click(function(){$.event.trigger('what')});

实时事件

delegate(); undelegate();

猜你喜欢

转载自blog.csdn.net/qq_38263696/article/details/83089403
今日推荐