2

我在弹出窗口中使用颜色框,并且...

这工作正常。

$(".show_popup").colorbox({inline:true, width:"800px", onOpen: function() {
   type = $(this).attr('type);
   ....
}

但我想多次使用我的内部函数,所以我想把它变成一个模块函数。

});

(function ($,a) {
 var p = {
   showPopup: function (popup_type) { 
    ...
   },

   bindEvents: function () {
       $(".show_popup").colorbox({inline:true, width:"800px", onOpen: p.showPopup($(this).attr('type')) }); 
   }
...
}
a.Popups = p;
})(jQuery);

但这不起作用 - 这是问题$(this)- 并且功能仅在页面加载后执行一次。

(function ($,a) {
  var p = {
   showPopup: function (popup_type) { 
    ...
   },

   bindEvents: function () {
       $(".show_popup").colorbox({inline:true, width:"800px", onOpen: p.showPopup }); 
   }
...
}
a.Popups = p;

})(jQuery);

这当然也不起作用,但要执行很多次。所以你能帮我知道什么是问题吗?

4

1 回答 1

1

问题onOpen: p.showPopup($(this).attr('type))在于它会在你将它绑定到 onOpen 的那一刻运行 p.showPopup 函数。您想要的是它在触发 onOpen 事件时运行。利用

onOpen: function() { p.showPopup($(this).attr('type')); }

反而

(编辑)假设 p.showPopup 已定义,我在您的代码中看不到它。

于 2013-11-13T20:06:48.163 回答