[DOM Event Learning] Section 3 jQuery事件处理基础 on(), off()和one()方法使用
[DOM Event Learning] Section 3 jQuery事件处理基础 on(),off()和one()方法使用
jQuery的事件绑定方法
// Event setup using a convenience method
$("p").click(function () {
console.log("You clicked a paragraph!");
});
// Equivalent event setup using the `.on()` method
$("p").on("click", function () {
console.log("click");
});
.on()方法
// Multiple events, same handler
$("input").on(
"click change", // Bind handlers for multiple events
function () {
console.log("An input was clicked or changed!");
}
);
// Binding multiple events with different handlers
$("p").on({
"click": function () {
console.log("clicked!");
},
"mouseover": function () {
console.log("hovered!");
}
});
$(document).ready(function () {
// Sets up click behavior on all button elements with the alert class
// that exist in the DOM when the instruction was executed
$("button.alert").on("click", function () {
console.log("A button with the alert class was clicked!");
});
// Now create a new button element with the alert class. This button
// was created after the click listeners were applied above, so it
// will not have the same click behavior as its peers
$("<button class='alert'>Alert!</button>").appendTo(document.body);
});
事件对象
// Event setup using the `.on()` method with data
$("input").on(
"change",
{foo: "bar"}, // Associate data with event binding
function (eventObject) {
console.log("An input value has changed! ", eventObject.data.foo);
}
);
var $element = $(this);
解除事件监听器
// Tearing down all click handlers on a selection
$("p").off("click"); // Tearing down a particular click handler, using a reference to the function
var foo = function () {
console.log("foo");
};
var bar = function () {
console.log("bar");
}; $("p").on("click", foo).on("click", bar);
$("p").off("click", bar); // foo is still bound to the click event
设置只执行一次的事件处理
// Switching handlers using the `.one()` method
$("p").one("click", firstClick); function firstClick() {
console.log("You just clicked this for the first time!"); // Now set up the new handler for subsequent clicks;
// omit this step if no further click responses are needed
$(this).click(function () {
console.log("You have clicked this before!");
});
}
// Using .one() to bind several events
$("input[id]").one("focus mouseover keydown", firstEvent); function firstEvent(eventObject) {
console.log("A " + eventObject.type + " event occurred for the first time on the input with id " + this.id);
}
参考资料
[DOM Event Learning] Section 3 jQuery事件处理基础 on(), off()和one()方法使用的更多相关文章
- [DOM Event Learning] Section 2 概念梳理 什么是事件 DOM Event
[DOM Event Learning] Section 2 概念梳理 什么是事件 DOM Event 事件 事件(Event)是用来通知代码,一些有趣的事情发生了. 每一个Event都会被一个E ...
- [DOM Event Learning] Section 1 DOM Event 处理器绑定的几种方法
[DOM Event Learning] Section 1 DOM Event处理器绑定的几种方法 网页中经常需要处理各种事件,通常的做法是绑定listener对事件进行监听,当事件发生后进行一 ...
- [DOM Event Learning] Section 4 事件分发和DOM事件流
[DOM Event Learning] Section 4 事件分发和DOM事件流 事件分发机制: event dispatch mechanism. 事件流(event flow)描述了事件对象在 ...
- Jquery学习笔记:事件处理基础介绍
一.引子 给html的元素添加一个响应事件,最简单的办法是直接在元素标签内填写事件属性,先看一个最简单的例子 <!DOCTYPE html> <html lang="zh- ...
- 第一百六十六节,jQuery,基础 DOM 和 CSS 操作,元素内容,元素属性,css和class,元素宽度高度、偏移、滚动条
jQuery,基础 DOM 和 CSS 操作,元素内容,元素属性,css和class,元素宽度高度.偏移.滚动条 学习要点: 1.DOM 简介 2.设置元素及内容 3.元素属性操作 4.元素样式操作 ...
- jQuery基础修炼圣典—DOM篇(二)jQuery遍历
1.children()方法 jQuery是一个合集对象,如果想快速查找合集里面的第一级子元素,此时可以用children()方法.这里需要注意:.children(selector) 方法是返回匹配 ...
- jQuery编程基础精华01(jQuery简介,顶级对象$,jQuery对象、Dom对象,链式编程,选择器)
jQuery简介 什么是jQuery? jQuery就是一个JavaScript函数库,没什么特别的.(开源)联想SQLHelper类 jQuery能做什么?jQuery是做什么的? jQuery本身 ...
- JavaScript 基础(四) - HTML DOM Event
HTML DOM Event(事件) HTML 4.0 的新特性之一是有能力使 HTML 事件触发浏览器中的动作(action),比如当用户点击某个 HTML 元素时启动一段 JavaScript.下 ...
- JavaScript学习 - 基础(七) - DOM event(事件)
DOM event(事件) 定义事件: // 定义事件: //方式一,直接在标签上定义事件 // 方式二: var a11 = document.getElementsByName('a11')[0] ...
随机推荐
- MySQL 外键
在MySQL中 (1)MySQL 数据表主要支持六种类型 ,分别是:BDB.HEAP.ISAM.MERGE.MYISAM.InnoBDB.这六种又分为两类,一类是”事务安全型”(transaction ...
- javascript面向对象系列第一篇——构造函数和原型对象
× 目录 [1]构造函数 [2]原型对象 [3]总结 前面的话 一般地,javascript使用构造函数和原型对象来进行面向对象编程,它们的表现与其他面向对象编程语言中的类相似又不同.本文将详细介绍如 ...
- 【开源】OSharp3.3框架解说系列:重新开源及3.3版本新特性
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- 我被比特币撞了一下腰——记OKCoin试用体验
本博客还有大量的.NET开源技术文章,您可能感兴趣: 1.开源Math.NET基础数学类库使用系列文章:链接 2.开源C#彩票数据资料库系列文章:链接 3.开源的.NET平台ORM组件文章:链接 4. ...
- IOS开发之TabBarItem&NavigationBarItem
想必大家都用过微信,微信间的页面切换是如何做成的呢?接下来我们用storyboard结合着代码来模拟一下微信的视图控制模式. "工欲善其事,必先利其器",下面主要是对storybo ...
- YII 的源码分析(二)
上一篇简单分析了一下yii的流程,从创建一个应用,到屏幕上输出结果.这一次我来一个稍复杂一点的,重点在输出上,不再是简单的一行"hello world",而是要经过view(视图) ...
- 字符串模式匹配之KMP算法图解与 next 数组原理和实现方案
之前说到,朴素的匹配,每趟比较,都要回溯主串的指针,费事.则 KMP 就是对朴素匹配的一种改进.正好复习一下. KMP 算法其改进思想在于: 每当一趟匹配过程中出现字符比较不相等时,不需要回溯主串的 ...
- objective-c中的@selector()和 c /c++的函数指针
先看tomcat里用到的代码: //然后开始动画 //把图片放到animationImages,接受数组参数 self.tom.animationImages = arrayImage; //设置时间 ...
- ReactJS分析之入口函数render
前言 在使用React进行构建应用时,我们总会有一个步骤将组建或者虚拟DOM元素渲染到真实的DOM上,将任务交给浏览器,进而进行layout和paint等步骤,这个函数就是React.render() ...
- 再看ftp上传文件
前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...