[英]Some problems with Javascript functions and this argument
我正在使用colorbox彈出窗口...
這很好。
$(".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);
當然這也不起作用,但是執行了很多次。 那你能幫我知道怎么回事嗎?
onOpen: p.showPopup($(this).attr('type))
的問題onOpen: p.showPopup($(this).attr('type))
是在將其綁定到onOpen時將運行p.showPopup-function。 您想要的是它在觸發onOpen事件時運行。 采用
onOpen: function() { p.showPopup($(this).attr('type')); }
代替
(edit)假設已定義p.showPopup,我在您的代碼中看不到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.