本教程操作环境:windows7系统、jquery3.2.1版本,Dell G3电脑。
推荐:jquery视频教程
jquery处理事件的方法:
事件处理程序的简单注册
//单击任意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();