Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript函数和此参数的一些问题_Javascript_Function_This_Dom Events_Colorbox - Fatal编程技术网

Javascript函数和此参数的一些问题

Javascript函数和此参数的一些问题,javascript,function,this,dom-events,colorbox,Javascript,Function,This,Dom Events,Colorbox,我正在使用我的弹出窗口和颜色框 这个很好用 $(".show_popup").colorbox({inline:true, width:"800px", onOpen: function() { type = $(this).attr('type); .... } 但是我想多次使用我的内部函数,所以我想把它变成一个模块函数 })) 但这不起作用-这是$(this)的问题-函数在页面加载后只执行一次 (function ($,a) { var p = { showPopup

我正在使用我的弹出窗口和颜色框

这个很好用

$(".show_popup").colorbox({inline:true, width:"800px", onOpen: function() {
   type = $(this).attr('type);
   ....
}
但是我想多次使用我的内部函数,所以我想把它变成一个模块函数

}))

但这不起作用-这是
$(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);

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

onOpen:p.showPopup($(this).attr('type))的问题是,它将在你将p.showPopup-function绑定到onOpen时运行。您想要的是它在onOpen事件触发时运行。使用

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


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

不太清楚您在做什么或您的问题是什么。请在jsbin.com之类的服务上制作一个示例,只包含与您的问题相关的内容。这使得人们更容易帮助你。bfavaretto,我已经使我的代码更加完整。很抱歉复制-粘贴:)
onOpen: function() { p.showPopup($(this).attr('type')); }