jquery学习手记(10)事件简介
1. 使用jquery监听的方法有许多种:
// The many ways to bind events with jQuery // Attach an event handler directly to the button using jQuery's // shorthand `click` method. $( "#helloBtn" ).click(function( event ) {alert( "Hello." ); });// Attach an event handler directly the to button using jQuery's // `bind` method, passing it an event string of `click` $( "#helloBtn" ).bind( "click", function( event ) {alert( "Hello." ); });// As of jQuery 1.7, attach an event handler directly to the button // using jQuery's `on` method. $( "#helloBtn" ).on( "click", function( event ) {alert( "Hello." ); });// As of jQuery 1.7, attach an event handler to the `body` element that // is listening for clicks, and will respond whenever *any* button is // clicked on the page. $( "body" ).on({click: function( event ) {alert( "Hello." );} }, "button" );// An alternative to the previous example, using slightly different syntax. $( "body" ).on( "click", "button", function( event ) {alert( "Hello." ); });
2. 事件对象
// Preventing a default action from occurring and stopping the event bubbling $( "form" ).on( "submit", function( event ) {// Prevent the form's default submission. event.preventDefault();// Prevent event from bubbling up DOM tree, prohibiting delegation event.stopPropagation();// Make an AJAX request to submit the form data });
3.事件处理
jquery的.on()方法提供了一些有用的特点:
3.1 一对一的事件绑定
// When any <p> tag is clicked, we expect to see '<p> was clicked' in the console. $( "p" ).on( "click", function() {console.log( "<p> was clicked" ); });
3.2 一对多的事件绑定
// When a user focuses on or changes any input element, we expect a console message // bind to multiple events $( "div" ).on( "mouseenter mouseleave", function() {console.log( "mouse hovered over or left a div" ); });
3.3 多对多的事件绑定
$( "div" ).on({mouseenter: function() {console.log( "hovered over a div" );},mouseleave: function() {console.log( "mouse left a div" );},click: function() {console.log( "clicked on a div" );} });
3.4 事件对象
$( "div" ).on( "click", function( event ) {console.log( "event object:" );console.dir( event ); });
3.5 向事件处理中传入数据
$( "p" ).on( "click", {foo: "bar" }, function( event ) {console.log( "event data: " + event.data.foo + " (should be 'bar')" ); });
3.6 事件代理
$( "ul" ).on( "click", "li", function() {console.log( "Something in a <ul> was clicked, and we detected that it was an <li> element." ); });
3.7 只运行一次的事件
// Switching handlers using the `.one()` method $( "p" ).one( "click", function() {console.log( "You just clicked this for the first time!" );$( this ).click(function() {console.log( "You have clicked this before!" );}); });
3.8 关闭事件
// Unbinding 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 );// foo will stay bound to the click event $( "p" ).off( "click", bar );
转载于:https://www.cnblogs.com/davidwang456/archive/2013/05/12/3073803.html
jquery学习手记(10)事件简介相关推荐
- JQuery学习22篇(事件委托)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- jQuery学习笔记:事件
一.页面载入 1.ready(fn) 当DOM载入就绪可以查询及操纵时绑定一个要执行的函数. 这是事件模块中最重要的一个函数,因为它可以极大地提高web应用程序的响应速度. 简单地说,这个方法纯粹是对 ...
- jquery学习手记(9)事件基础知识
1. jquery事件机制提供了两个事件相关的帮助函数: $.fn.hover 提供一个或者两个传入函数参数 // The hover helper function $( "#menu l ...
- 70天的JQUERY学习: 选择器+事件+效果。总结篇
运行结果 这是我等下要发的代码.首先去了解Jquery的Apl:APL代码. <!DOCTYPE html> <html class="no-js loading-pr ...
- jquery学习手记(4)元素的选择与操作
一. 选择元素 id选择器,示例为: 1 // Selecting elements by ID 2 $("#myId"); // note IDs must be unique ...
- jquery学习手记(1)
1.$ vs $() $()方法中的参数是jQuery选择器并调用该选择器的方法,格式如下所示: $.fn,例子:$("h1").remove();自动接收和返回该选择器的动作. ...
- [JQuery 学习笔记] 解除事件绑定 unbind()
unbind() : 解除事件绑定 <!DOCTYPE html> <html lang="en"> <head><meta chars ...
- jquery学习手记(7)Data_utility_index方法
Data方法 Js对dom元素增加一个属性时,你必须处理一些浏览器内存泄漏的问题.Jquery提供了一个元素保存数据的方法,该方替你管理内存问题.示例: // Storing and retrievi ...
- jquery学习手记(5)对象
DOM和DOM元素 DOM是html文件的表现层,它包含了很多DOM元素,宏观上来讲,DOM元素可以被认为是web页面的一个片段.DOM的形式有类型如<div>, <a>, 或 ...
最新文章
- CentOS 6.9/7通过yum安装指定版本的JDK/Maven
- 给你一个热爱阅读的机会,走到哪儿,看到哪儿的读书体验
- mysql 硬解析 软解析_ORACLE sql语句的硬解析与软解析(收藏)
- layui table 字体大小_layui table设置某一行的字体颜色方法
- 如何挖掘消费者的隐性需求?
- win32应用程序_电脑打不开exe程序|Win10提示exe不是有效32应用程序
- uibmodal模态框打开另一个模态框_进阶版神笔:只需一句话、一个故事,即可生成画面...
- 用VirtualWifi软件实现无线网卡同时连接多个AP。
- 问题 1049: [编程入门]结构体之时间设计
- 从编译到执行,C++如何开发SIMD友好的代码?
- 通常情况下的中国剩余定理
- vue使用element-ui的el-input监听不了回车事件解决
- Mac双系统中Windows无法使用苹果鼠标键盘等问题的解决方法
- 数学也荒唐:20个脑洞大开的数学趣题
- 21.	SCHEMATA
- RNA-seq Review:RNA-seq数据分析
- 我的物联网大学【第五章】:沉默不会爆发,沉默一定会灭亡!
- 微信小程序之如何注册微信小程序
- android后台通过View生成分享图片
- Linux下串口编程总结
热门文章
- python最大堆_用Python实现最大堆
- 大学计算机组装与维修考试题,【校选修】计算机组装与维修 考试题
- python制表符什么意思_python中制表符是什么意思
- 深度linux腾讯视频,在UOS/Deepin 20/Ubuntu 18.04下安装腾讯视频Linux版的方法
- pymysql dataframe 写入sql
- 联想 facebook android,Lenovo Vantage
- 103. Leetcode 213. 打家劫舍 II (动态规划-打家劫舍)
- 向有环的环形链表中插入新节点
- python编程基础(四):编程习惯、代码规范、易混淆之处
- python笔记:jieba(中文分词)