jQuery事件命名空間的深入分析

來源:互聯網
上載者:User

命名空間
複製一份維基百科的說法:

命名空間(英語:Namespace)表示標識符(identifier)的可見範圍。一個標識符可在多個命名空間中定義,它在不同命名空間中的含義是互不相干的。這樣,在一個新的命名空間中可定義任何標識符,它們不會與任何已有的標識符發生衝突,因為已有的定義都處於其它命名空間中。

簡單點說:

假如網吧裡有好多人,都沒有名字的話,你媽媽喊你回家吃飯的時候,該怎麼叫誰呢? 所以聰明的媽媽,給你起了一個名字,下次媽媽只需要站在門口大喊一聲“xx,回家吃飯了!”

jQuery命名空間的出現
注意這裡說的只是jquery1.7,因為它推薦大家使用 on 代替以前的 bind,使用 off 代替以前的 unbind。因為所有的事件都是通過 on 來綁定的。

好嘞!綁一個試試

 代碼如下 複製代碼
$(document).on('click', function(){
  ....
});

挺好用,為了不讓它多次觸發,我要解除綁定掉

 代碼如下 複製代碼
$(document).off('click')

細心的同學可能已經發現了,如果我只想解除綁定某個特定的事件,就比較蛋疼了,舉個栗子!

 代碼如下 複製代碼
$(document).on('click', function(){
  console.log(1111);
});
$(document).on('click', function(){
  console.log(222);
});
$(document).on('click', function(){
  console.log(333);
});

我覺得 列印出111的比較不合群,想踢出去,這時候會不會很無語呢?

聰明的你馬上想到 我可以這樣啊!

 代碼如下 複製代碼

function a(){
  console.log(1111); 
}

$(document).on('click', a);
$(document).off('click', a);

是滴! 你可以這樣!但是你不覺得很麻煩嗎?

我們可以這樣

 代碼如下 複製代碼
$(document).on('click.a', function(){
  console.log(123);
})
$(document).off('click.a');

該方法最早出現在jQuery1.3.2中

代碼實現如下(1.3.2中的實現):

 代碼如下 複製代碼

// Namespaced event handlers
var namespaces = type.split(".");
type = namespaces.shift();
handler.type = namespaces.slice().sort().join(".");

// Get the current list of functions bound to this event
var handlers = events[type];

if ( jQuery.event.specialAll[type] )
  jQuery.event.specialAll[type].setup.call(elem, data, namespaces);

// Init the event handler queue
if (!handlers) {
  handlers = events[type] = {};

  // Check for a special event handler
  // Only use addEventListener/attachEvent if the special
  // events handler returns false
  if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem, data, namespaces) === false ) {
    // Bind the global event handler to the element
    if (elem.addEventListener)
      elem.addEventListener(type, handle, false);
    else if (elem.attachEvent)
      elem.attachEvent("on" + type, handle);
  }
}

命名空間配合trigger的使用

 代碼如下 複製代碼

$(document).on('click.a', funciton(){
  console.log(111); 
});
$(document).on('click', funciton(){
  console.log(222); 
});

$(document).trigger('click.a');

//output: 111

還可以這樣

 代碼如下 複製代碼

$(document).on('click.a', funciton(){
  console.log(111); 
});
$(document).on('click', funciton(){
  console.log(222); 
});

$(document).trigger('click!');

//output: 222

注意:
! 在jquery1.9之後被刪除相應實現

事件綁定命名空間

命名空間方式的一般使用:

 代碼如下 複製代碼

$('.class').bind('click.namespace', function(){});
$('.class').trigger('click.namespace');
$('.class').unbind('click.namespace');


同一個命名空間的事件可以直接用命名空間批量解除綁定。

 代碼如下 複製代碼

$('.class').bind('click.namespace', function(){});
$('.class').bind('blur.namespace', function(){});
$('.class').unbind('.namespace');

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.