首頁 > web前端 > js教程 > 主體

jQuery on()方法範例及優點

巴扎黑
發布: 2017-06-25 10:37:43
原創
1171 人瀏覽過

使用jquery on()方法綁定事件是官方推薦的一種方法,接下來跟著小編來學習jquery on()方法,小夥伴快來一起學習吧

jQuery on()方法是官方推薦的綁定事件的一個方法。

$(selector).on(event,childSelector,data,function,map)
登入後複製

由此擴展開來的幾個以前常見的方法有.

bind()

 $("p").bind("click",function(){
    alert("The paragraph was clicked.");
  });
  $("p").on("click",function(){
    alert("The paragraph was clicked.");
  });
登入後複製

delegate()

$("#p1").on("click","p",function(){
    $(this).css("background-color","pink");
  });
  $("#p2").delegate("p","click",function(){
    $(this).css("background-color","pink");
  });
登入後複製

live()

  $("#p1").on("click",function(){
    $(this).css("background-color","pink");
  });
  $("#p2").live("click",function(){
    $(this).css("background-color","pink");
  });
登入後複製

以上三種方法在jQuery1.8之後都不推薦使用,官方在 1.9時已經取消使用live()方法了,所以建議都使用on()方法。

tip:如果你需要移除on()所綁定的方法,可以用off()方法處理。

$(document).ready(function(){
  $("p").on("click",function(){
    $(this).css("background-color","pink");
  });
  $("button").click(function(){
    $("p").off("click");
  });
});
登入後複製

tip:如果你的事件只需要一次的操作,可以用one()這個方法

$(document).ready(function(){
  $("p").one("click",function(){
    $(this).animate({fontSize:"+=6px"});
  });
});
登入後複製

trigger()綁定

$(selector).trigger(event,eventObj,param1,param2,...)
$(document).ready(function(){
  $("input").select(function(){
    $("input").after(" Text marked!");
  });
  $("button").click(function(){
    $("input").trigger("select");
  });
});
登入後複製

多個事件綁定同一個函數

$(document).ready(function(){
 $("p").on("mouseover mouseout",function(){
  $("p").toggleClass("intro");
 });
});
登入後複製

多個事件綁定不同函數

$(document).ready(function(){
 $("p").on({
  mouseover:function(){$("body").css("background-color","lightgray");}, 
  mouseout:function(){$("body").css("background-color","lightblue");}, 
  click:function(){$("body").css("background-color","yellow");} 
 });
});
登入後複製

綁定自訂事件

$(document).ready(function(){
 $("p").on("myOwnEvent", function(event, showName){
  $(this).text(showName + "! What a beautiful name!").show();
 });
 $("button").click(function(){
  $("p").trigger("myOwnEvent",["Anja"]);
 });
});
登入後複製

#傳遞資料到函數

function handlerName(event) 
{
 alert(event.data.msg);
}
$(document).ready(function(){
 $("p").on("click", {msg: "You just clicked me!"}, handlerName)
});
登入後複製

適用於未建立的元素

$(document).ready(function(){
 $("p").on("click","p",function(){
  $(this).slideToggle();
 });
 $("button").click(function(){
  $("<p>This is a new paragraph.</p>").insertAfter("button");
 });
});
登入後複製

jQuery綁定事件的方法有幾種,建議使用.on()方法綁定,原因有兩點:

1. on()方法可以綁定動態加入到頁面元素的事件

例如動態加入到頁面的DOM元素,用.on()方法綁定的事件不需要關心註冊該事件的元素何時被加入進來,也不需要重複綁定。有的同學可能習慣於用.bind()、.live()或.delegate(),查看源碼就會發現,它們實際上調用的都是.on()方法,並且.live()方法在jQuery1. 9版本已經被移除。


bind:
function(
 types, data, fn ) {
  return this.on(
 types, null,
 data, fn );
},
live:
function(
 types, data, fn ) {
  jQuery(
this.context
 ).on( types, this.selector,
 data, fn );
  return this;
},
delegate:
function(
 selector, types, data, fn ) {
  return this.on(
 types, selector, data, fn );
}
登入後複製

移除.on()綁定的事件用.off()方法。

2.on()方法綁定事件可以提升效率

很多文章都提到了利用事件冒泡和代理來提升事件綁定的效率,大多都沒列出具體的差別,所以為了求證,我做一個小測試。

假設頁面新增了5000個li,用chrome開發者工具Profiles測試頁面載入時間。

普通綁定(姑且這麼稱呼它)


$(&#39;li&#39;).click(function(){
  console.log(this)
});
登入後複製

綁定過程的執行時間

2013 -08-13_190358

普通綁定相當於在5000li上面分別註冊click事件,記憶體佔用約4.2M,綁定時間約72ms。

.on()綁定


$(document).on(&#39;click&#39;,
&#39;li&#39;,
function(){
  console.log(this)
})
登入後複製

綁定程序的執行時間

2013-08-13_191010

.on()綁定利用事件代理,只在document上註冊了一個click事件,記憶體佔用約2.2M,綁定時間約為1ms。

以上是jQuery on()方法範例及優點的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!