jquery学习手记(10)事件简介
来源:互联网 发布:如何拍评价蛤蟆知乎 编辑:程序博客网 时间:2024/05/22 06:37
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 functionvar 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 );
- jquery学习手记(10)事件简介
- jquery学习手记(9)事件基础知识
- jquery学习手记(1)
- jquery学习手记(3)属性
- jquery学习手记(5)对象
- jquery学习手记(7)Data_utility_index方法
- jquery学习手记(8)遍历
- jQuery基础学习一 简介、选择器、事件
- JavaScript学习手记事件学习
- jQuery简介Event事件
- jquery学习手记(4)元素的选择与操作
- jquery学习手记(6)CSS, Styling, & Dimensions
- jQuery学习(一):jQuery简介
- jQuery学习(三)事件
- jQuery学习(三) 事件
- jquery学习三(事件)
- .NET学习手记之:WPF--路由事件
- JavaScript学习手记3事件详解
- jquery学习手记(7)Data_utility_index方法
- jquery学习手记(8)遍历
- js继承的实现(转载)
- 【转】Scrum角色及其职责介绍
- jquery学习手记(9)事件基础知识
- jquery学习手记(10)事件简介
- jstl核心标签使用
- jstl sql标签使用
- 论做人与做事
- Debugging WebLogic Server Applications Using Eclipse and the WebLogic-Plugin
- linux 自动重启tomcat 脚本
- Flex Tree组件
- struts学习
- struts深入原理之RequestProcessor与xml