当ThickBox关闭时,您将如何触发事件?
这个问题与Wordpress有半个相关,但在其他地方也有应用。 基本上,我试图这样做,当有人退出Thickbox时,它会触发页面上其他地方的事件。 编辑Thickbox文件不是一个选项。
这有点复杂,因为Thickbox不是这样编写的。 但也许你可以使用一些技巧来做到这一点。
这不是推荐的解决方案,但您可以“重写”关闭function。 就像是:
var old_tb_remove = window.tb_remove; var tb_remove = function() { old_tb_remove(); // calls the tb_remove() of the Thickbox plugin alert('ohai'); };
Works \ o / http://jsfiddle.net/8q2JK/1/
你可以试试这样的……
var tb_unload_count = 1; $(window).bind('tb_unload', function () { if (tb_unload_count > 1) { tb_unload_count = 1; } else { // do something here tb_unload_count = tb_unload_count + 1; } });
tb_unload
被触发两次,所以这包括一些黑客,以确保您的代码不会第二次运行。
我认为您可以通过将单击处理程序绑定到关闭按钮来破解它:
$("#TB_closeWindowButton").click(function() { doSomething(); });
如果您有选择,请摆脱thickbox(因为它不再维护)并使用更活跃的社区。 事实上,thickbox网站提出了一些替代 镜像 。
在Thickbox 3.1上,我将进入thickbox-fixed.js并为我自己添加一个自定义函数
只是添加一个回调函数,不确定是一个好方法,但它的工作对我的项目。
function mycustom_tb_remove(callback) { $("#TB_imageOff").unbind("click"); $("#TB_closeWindowButton").unbind("click"); $("#TB_window").fadeOut("fast",function(){ if(callback)callback(); $('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove(); }); $("#TB_load").remove(); if (typeof document.body.style.maxHeight == "undefined") {//if IE 6 $("body","html").css({height: "auto", width: "auto"}); $("html").css("overflow",""); } document.onkeydown = ""; document.onkeyup = ""; return false; }
所以当我使用我的自定义删除function时。 我会用这种方式。
self.parent.mycustom_tb_remove(function(){ alert("Closed"); });
不确定这是否是全局解决方案,但对于WordPress,至少对于最新版本(4.9.5),您可以使用:
jQuery( 'body' ).on( 'thickbox:removed', function() { // Your code here. }
在Thickbox v3.1中,tb_remove()调用:
jQuery( 'body' ).trigger( 'thickbox:removed' );
请参阅: https : //github.com/WordPress/WordPress/blob/master/wp-includes/js/thickbox/thickbox.js#L290
希望有所帮助!
我想在打开厚盒子时触发一个事件我能够这样做:(希望它有助于某人)
jQuery( 'body' ).on( 'thickbox:iframe:loaded', function ( event ) { //Your trigger code here.
});
您可以将侦听器绑定到“tb_unload”,该窗口在关闭厚箱时触发。 使用jQuery的示例:
jQuery('#tb_button').on('click', function(){ tb_show('This is Google in a thickbox', 'http://www.google.com?TB_iframe=true'); jQuery('#TB_window').on("tb_unload", function(){ alert('Triggered!'); }); }