jquery学习手记(10)事件简介
生活随笔
收集整理的這篇文章主要介紹了
jquery学习手记(10)事件简介
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?1. 使用jquery監(jiān)聽(tīng)的方法有許多種:
// 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. 事件對(duì)象
// 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()方法提供了一些有用的特點(diǎn):
?3.1 一對(duì)一的事件綁定
// 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 一對(duì)多的事件綁定
// 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 多對(duì)多的事件綁定
$( "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 ?事件對(duì)象
$( "div" ).on( "click", function( event ) {console.log( "event object:" );console.dir( event ); });3.5 向事件處理中傳入數(shù)據(jù)
$( "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 只運(yùn)行一次的事件
// 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 關(guān)閉事件
// 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 );?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/archive/2013/05/12/3073803.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的jquery学习手记(10)事件简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: jquery学习手记(9)事件基础知识
- 下一篇: jstl核心标签使用