我這就這段代碼定義插件實例:功能jQuery插件裏面調用
$.fn.someplugin = function(opts) {
$(document).on('click', '.option-1', function() {
alert(1);
});
};
我使用一些代碼,這樣一個讓我的插件工作:
$('.selector-1').someplugin();
所以在jQuery的這種方式將可能的一個點擊事件監聽器綁定到文檔。
問題是,當我多次使用插件時,這是否意味着jQuery將10個單擊事件綁定到文檔?
$('.selector-1').someplugin();
$('.selector-2').someplugin();
$('.selector-3').someplugin();
$('.selector-4').someplugin();
$('.selector-5').someplugin();
$('.selector-6').someplugin();
$('.selector-7').someplugin();
$('.selector-8').someplugin();
$('.selector-9').someplugin();
$('.selector-10').someplugin();
通過這種方式,它綁定了10個點擊監聽器 - 因爲fn.someplugin被調用了10次,或者只是一個?
該死的,這是非常糟糕的。 – Jasper
@Steve爲什麼它真的很糟糕?你能指望什麼? –
我想盡量減少事件處理程序,因爲我的頁面上可能會有超過200個插件調用。當時性能開始下滑。 – Jasper