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下 安装xampp
- 网站架构相关PPT、文章整理(更新于2009-7-15)
- NB-IoT在智能农业中的示范应用
- 访问量大如何增加服务器,服务器流量过大原因及解决方法
- C#调用C++类(以COM组件的形式)
- .net core中不支持GB2312编码的问题
- 啊!多么痛的领悟···
- docker--shell和Exec格式
- csgo天津服务器维护怎么办,CSGO华北赛区在天津落下帷幕
- 第一章节 初识C#程序
- 大数据分析给企业带来哪些挑战
- vuetify模板零基础入门
- 医疗病案首页网上直报编码汇总
- border 属性样式
- java n_javan是什么意思_javan怎么读_javan翻译_用法_发音_词组_同反义词_爪哇人[语]-新东方在线英语词典...
- python 网络拓扑图_python 网络拓扑图
- java fact函数_Fact 定义一个类Fact表示无符号有理数(分数) 联合开发网 - pudn.com...
- 干货!mysql初始密码
- excel启用宏在哪里_Excel 2010在不启用的情况下运行宏
热门文章
- pythonsuper多重继承_小白都能理解的Python多继承
- linux mysql主主复制_MySQL主从复制与主主复制
- java xsl生成word文件_导出生成xsl文件
- android root 恢复出厂设置,Android系统 免root 卸载预置应用
- 远程服务器php环境搭建,免费网站搭建与phpstorm远程部署
- java cordova_java – Cordova android后台插件在5分钟后被杀死
- android原生组件,XUI: 一个简洁而优雅的Android原生UI框架,解放你的双手!
- mysql搭建主主_mysql主主配置
- 同济大学计算机学院徐老师,第十八届同济大学程序设计竞赛暨高校网络友谊赛圆满落幕...
- linux区分用户的权限级别可用,如何限制Linux内核级别的特权用户访问?